我使用以下代码发送电子邮件。有时它工作正常,有时它会产生错误。有没有更好的代码来发送电子邮件。还有一件事是必须提供发送邮件的密码。
using System.Net.Mail;
public void SendEmail()
{
MailMessage mail = new MailMessage();
mail.To.Add("sales@ojhatraders.com");
mail.From = new MailAddress("ojhatraderscustomer@gmail.com");
mail.Subject = "Contact Us Enquiry";
string Body = "<b>From:<b>" + mail.From + "<br/>" + "Your Query Recived "+"<br/>"+"Name"+nameTextBox.Text+"<br/>"+"Mobile:"+mobileTextBox.Text+"<br/>"
+"Email:"+emailTextBox.Text+"<br/>"+"Query:"+queryTextBox.Text;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("sample@gmail.com", "passsword");//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
请提供一些有用的建议和更好的代码。