[英]send email with smtp client in c#
我需要從我的Asp.net c#網站發送電子郵件,我們在ovh上托管,這是我的工作:
public static void Send(string stringFrom, string stringFromPass,
string stringTo, string stringBody, string stringSubject, int tryNb)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient("smtp.mondomaine.me", 587);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress(stringFrom,
"Sender",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress(stringTo);
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = stringBody;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = stringSubject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
//provide Authentication Details need to be passed when sending email from gmail
string password = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(stringFromPass));
NetworkCredential mailAuthentication = new NetworkCredential(stringFrom, password);
client.UseDefaultCredentials = false;
client.Credentials = mailAuthentication;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = stringFrom + "*&(k)&*" +
stringFromPass + "*&(k)&*" + stringTo + "*&(k)&*" +
stringBody + "*&(k)&*" + stringSubject + "*&(k)&*" + tryNb.ToString();
client.SendAsync(message, userState);
}
然后我在需要時稱呼此功能。 我收到OVH服務器響應:
sorry, invalid MAIL FROM for open-smtp session (http://travaux.ovh.com/?do=details&id=2602)
在最后一個鏈接上,他們告訴:
messages sent via open-smtp will only be accepted if the email address of the fields From / Sender is the same connection login used for POP access.
如您在我的代碼中看到的,我沒有使用POP,我也不知道該怎么做。 那為什么要這樣限制呢? 任何解決該問題的想法都將不勝枚舉。
如果要發送throw Gmail-Id
。
在您的電子郵件ID中更改電子郵件設置
遵循這些步驟
單擊1->設置
點擊2->轉發和POP / IMAP
選擇3->啟用IMAP訪問
郵寄功能:
MailMessage message = new MailMessage();
message.From = new MailAddress("Abc@Servername.com");
message.To.Add("Tosender@Servername.com");
message.Subject = "Your QR Code Image";
message.Body = "Please open this link and download this QR Code image for scanning your QRCode :" + QR_Code;
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
在WebConfig中:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="abc@gmail.com" password="****" />
</smtp>
</mailSettings>
</system.net>
問題是由mailAuthentication
引起的,現在我正在使用System.Security.SecureString
而不是Convert.ToBase64String
來編碼passwords
並且一切正常。
System.Security.SecureString secureString = new System.Security.SecureString();
stringFromPass.ToCharArray().ToList().ForEach(p => secureString.AppendChar(p));
NetworkCredential mailAuthentication = new NetworkCredential(stringFrom, secureString);
在Outlook中,您必須將smtp配置為使用電子郵件和密碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.