使用c#中的smtp客户端发送电子邮件
本文关键字:电子邮件 客户端 smtp 中的 使用 | 更新日期: 2023-09-27 18:26:25
我需要从我的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,我不知道如何使用。那么为什么要有这个限制呢?任何关于如何解决这一问题的想法都将受到极大的赞扬。
问题是由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配置为使用电子邮件和密码
如果要发送throw Gmail-Id
。
在您的电子邮件ID中更改电子邮件设置
按照以下步骤
-
单击1-->设置
-
单击2-->转发和POP/IMAP
-
Clect 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>