本文介绍了Reportviewer通过gmail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个内嵌有Reportviewer的WinForm.

I am working on a WinForm where a reportviewer is embedded.

如何通过gmail/yahoo发送电子邮件并将此报告作为pdf附件?我看了这篇文章 http://www.codeproject .com/Articles/32109/Send-Mail-and-Print-Report-in-Report-Viewer-Contro ,但不确定它是否仅适用于桌面电子邮件客户端(例如Outlook)或是否支持基于Web的电子邮件客户端?

How do I send email via gmail/yahoo and attach this report as a pdf?I looked at this posthttp://www.codeproject.com/Articles/32109/Send-Mail-and-Print-Report-in-Report-Viewer-Contro but was not sure if it would work only with desktop email clients such as Outlook or will it support web based email client?

提前谢谢!

推荐答案

const string HTML_TAG_PATTERN = "<.*?>";
static string StripHTML(string inputString)
{
  return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
}

public static void sendMessage()
{
  var username = "[email protected]";
  var password = "password";
  MailAddress MailFrom = new MailAddress("[email protected]");
  MailAddress MailTo = new MailAddress("[email protected]");
  var subject = "TEST SUBJECT";
  var attachmentPath = "test.pdf";
  var mailBody = "<b>test</b>";


  NetworkCredential cred = new NetworkCredential(username, password);

  System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
  smtp.Host = "smtp.gmail.com";
  smtp.UseDefaultCredentials = false;
  smtp.EnableSsl = true;
  smtp.Credentials = cred;
  smtp.Port = 587;

  MailMessage mail = new MailMessage();

  mail.IsBodyHtml = true;

  AlternateView avAlternateView = null;
  Encoding myEncoding = Encoding.GetEncoding("UTF-8");

  avAlternateView = AlternateView.CreateAlternateViewFromString(StripHTML(mailBody), myEncoding, "text/plain");
  mail.AlternateViews.Add(avAlternateView);

  avAlternateView = AlternateView.CreateAlternateViewFromString(mailBody, myEncoding, "text/html");
  mail.AlternateViews.Add(avAlternateView);

  mail.Sender = MailFrom;
  mail.From = MailFrom;
  mail.ReplyTo = MailFrom;

  mail.To.Add(MailTo);

  mail.Subject = subject;
  mail.SubjectEncoding = Encoding.GetEncoding("UTF-8");

  mail.BodyEncoding = Encoding.GetEncoding("UTF-8");

  Attachment attachment = new Attachment(attachmentPath);
  mail.Attachments.Add(attachment);
  try
  {
    smtp.Send(mail);
  }
  catch (Exception ex)
  {
  }
}

这篇关于Reportviewer通过gmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:36