我想向我的Gmail帐户发送一封电子邮件,其中包含文本框中的值。下面的代码是我试图做的但它没有工作你能告诉我可能是我的问题吗?当我运行该程序变得没有响应,它告诉我无法发送邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("myusername@gmail.com");
msg.To.Add("myusername@gmail.com");
msg.Subject = "test";
msg.Body = txtName.Text;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "www.gmail.com";
smtpClient.Send(msg);
答案 0 :(得分:0)
试试这个,
string mailServer, mailFrom;
int port;
string mailId, mailPass;
string subject;
StringBuilder mailBody = new StringBuilder();
subject = "subject";
mailBody.Length = 0;
mailBody.Append("<html><body><div>");
mailBody.Append(txtName.Text);
mailBody.Append("</div></body></html>");
mailServer = "smtp.gmail.com";
mailFrom = "something";
mailId = "example@gmail.com";
mailPass = "xxxxx";
port = 587;
MailMessage mail = new MailMessage(mailId, mailTo, subject, mailBody.ToString());
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer, port);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
使用适当的值更改电子邮件ID,密码和所有内容。