使用C#发送电子邮件 [英] Sending E-mail using C#

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

问题描述

我需要通过我的C#应用​​发送电子邮件。



我来自一个VB 6背景,并且对MAPI控件有很多糟糕的体验。
首先,MAPI不支持HTML电子邮件,其次,所有电子邮件都发送到我的默认邮件发件箱。所以我仍然需要点击发送接收。



如果我需要发送大量html身体电子邮件(100 - 200),那么最好的方法是C#?



提前感谢

解决方案

.NET框架的 System.Net.Mail.MailMessage



您可以找到 MSDN文档在这里



这是一个简单的例子(代码片段):

 使用System.Net; 
使用System.Net.Mail;
使用System.Net.Mime;

...
try
{

SmtpClient mySmtpClient = new SmtpClient(my.smtp.exampleserver.net);

//设置smtp-client with basicAuthentication
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential(username,password);
mySmtpClient.Credentials = basicAuthenticationInfo;

//从mailaddresses添加
MailAddress from = new MailAddress(test@example.com,TestFromName);
MailAddress to = new MailAddress(test2@example.com,TestToName);
MailMessage myMail = new System.Net.Mail.MailMessage(from,to);

//添加ReplyTo
MailAddress replyto = new MailAddress(reply@example.com);
myMail.ReplyToList.Add(replyTo);

//设置主题和编码
myMail.Subject =测试消息;
myMail.SubjectEncoding = System.Text.Encoding.UTF8;

//设置正文消息和编码
myMail.Body =< b>测试邮件< / b>< br>使用< b> HTML< / b> ;. ;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text或html
myMail.IsBodyHtml = true;

mySmtpClient.Send(myMail);
}

catch(SmtpException ex)
{
抛出新的ApplicationException
(SmtpException已发生:+ ex.Message);
}
catch(Exception ex)
{
throw ex;
}


I need to send email via my C# app.

I come from a VB 6 background and had a lot of bad experiences with the MAPI control. First of all, MAPI did not support HTML emails and second, all the emails were sent to my default mail outbox. So I still needed to click on send receive.

If I needed to send bulk html bodied emails (100 - 200), what would be the best way to do this in C#?

Thanks in advance.

解决方案

You could use the System.Net.Mail.MailMessage class of the .NET framework.

You can find the MSDN documentation here.

Here is a simple example (code snippet):

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("test@example.com", "TestFromName");
   MailAddress to = new MailAddress("test2@example.com", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyto = new MailAddress("reply@example.com");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;

   // set body-message and encoding
   myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
   myMail.BodyEncoding = System.Text.Encoding.UTF8;
   // text or html
   myMail.IsBodyHtml = true;

   mySmtpClient.Send(myMail);
}

catch (SmtpException ex)
{
  throw new ApplicationException
    ("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
   throw ex;
}

这篇关于使用C#发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆