小编Sam*_*Sam的帖子

如何使用Gmail SMTP服务器在C#中邮寄?

可能重复:
使用C#通过Gmail SMTP服务器发送电子邮件

对于使用C#进行邮寄并使用Gmail SMTP服务器,我们应该做些什么?因为在经过大量搜索之后我找到了一些方法来做到这一点,但结果却出现了失败异常.我想这是因为我没有为Gmail处理TSL(因为它适用于TSL),但我不知道如何使用C#来处理TSL.我非常感谢任何帮助或链接到有用的样本.这是我的代码:

public string SendMail(string senderMail, string receiverMail, string attachmentPath)
{
  var fromMailAddress = new MailAddress(senderMail);
  var toMailAddress = new MailAddress(receiverMail);

  MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress);
  mailMessage.Subject = "My Subject";
  mailMessage.Body = "This is the body of this message for testing purposes";

  Attachment attachFile = new Attachment(attachmentPath);
  mailMessage.Attachments.Add(attachFile);

  SmtpClient emailClient = new SmtpClient();

  NetworkCredential credential = new NetworkCredential();
  credential.UserName = fromMailAddress.User;
  credential.Password = "password";

  emailClient.Credentials = credential;
  emailClient.Port = 587;
  emailClient.Host = "smtp.gmail.com";

  //emailClient.EnableSsl = true; …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
2
解决办法
1231
查看次数

标签 统计

c# ×1