[英]C# send e-mail error. How to send an e-mail with this way?
此代碼未運行。 我想發送郵件,但我不能。
當我運行代碼時,我想發送一封電子郵件。 但它不起作用。 我真的需要幫助。
public static void SendNotification(string filepath)
{
try
{
SmtpClient mailServer = new SmtpClient(ConfigurationManager.AppSettings["host"], int.Parse(ConfigurationManager.AppSettings["portnumber"]));
mailServer.EnableSsl = true;
System.Net.NetworkCredential(ConfigurationManager.AppSettings["sender_username"], ConfigurationManager.AppSettings["sender_password"]);
string to = ConfigurationManager.AppSettings["RECEIVE"];
string cc = ConfigurationManager.AppSettings["CC"];
MailMessage msg = new MailMessage(from, to);
msg.Subject = "";
msg.Body = "Test Mail.";
mailServer.Send(msg);
}
catch (Exception ex)
{
//Log
}
}
你有遺漏的東西。
這是
private static void SendMail(string subject, string content)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("YOURMAİL");
mail.To.Add("MAİLTO");
mail.Subject = subject;
mail.Body = content;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("YOURMAİL", "YOURMAİLPASSWORD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
}
}
這是發送郵件的最簡單方法。 不要忘記使用 System.Net.Mail 添加; 您需要添加mail.From
。 這很重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.