首页 新闻 会员 周边 捐助

C#如何实现电子邮箱发送

0
悬赏园豆:20 [已解决问题] 解决于 2009-01-01 22:29

这2天自己在写电子邮件,老是实现不了,没办法了,来求助,谁给一份测试通过的代码参考,

简单就好了!谢谢!!

问题补充: 用了很多例子,都不行是不是我的机子有问题,老是报的错误是System.Net.Mail.SmtpException 中第一次偶然出现的“System.dll”类型的异常 邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨
零度吹风的主页 零度吹风 | 初学一级 | 园豆:0
提问于:2009-01-01 13:37
< >
分享
最佳答案
0

下面是vs2008中写的代码,测试成功,可以根据自己的需要稍作修改。如果是你是一的是.NET2.0,是不支持自带属性的,要把属性部分代码修改一下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace ISACA.Services.Common
{
public partial class MailEntity
{
/// <summary>
/// default constructor
/// </summary>
public MailEntity(){}

/// <summary>
/// Mail Entity constructor using 'GB2312' as default Encoding/timeout is 999999/IsHtmlBody is default as true
/// </summary>
/// <param name="mailFrom">the email address that will send the email</param>
/// <param name="userName">the name of the user who send the email</param>
/// <param name="pwd">the password of the user who send the email</param>
/// <param name="displaySenderName">the sender name</param>
/// <param name="mailTo">the receiver email address</param>
/// <param name="displayReceiverName">the receiver name</param>
/// <param name="subject">the subject of the email</param>
/// <param name="contentBody">the content of the email</param>
/// <param name="host">the host address that send email</param>
public MailEntity(string mailFrom, string userName, string pwd, string displaySenderName, string mailTo, string displayReceiverName, string subject, string contentBody, string host)
{
TimeOut
= 999999;
Encoding
= Encoding.GetEncoding("GB2312"); ;
MailFrom
= mailFrom;
UserName
= userName;
Pwd
= pwd;
DisplaySenderName
= displaySenderName;
MailTo
= mailTo;
DisplayReceiverName
= displayReceiverName;
Subject
= subject;
ContentBody
= contentBody;
Host
= host;
IsBodyHtml
= true;
}

/// <summary>
/// Mail Entity constructor
/// </summary>
/// <param name="timeOut">the email send timeout</param>
/// <param name="encoding">the Encoding type that will edcode the email content</param>
/// <param name="mailFrom">the email address that will send the email</param>
/// <param name="userName">the name of the user who send the email</param>
/// <param name="pwd">the password of the user who send the email</param>
/// <param name="displaySenderName">the sender name</param>
/// <param name="mailTo">the receiver email address</param>
/// <param name="displayReceiverName">the receiver name</param>
/// <param name="subject">the subject of the email</param>
/// <param name="contentBody">the content of the email</param>
/// <param name="host">the host address that send email</param>
/// <param name="isBodyHtml">if the content of the email is Html</param>
public MailEntity(System.Int32 timeOut, System.Text.Encoding encoding, string mailFrom, string userName, string pwd, string displaySenderName, string mailTo, string displayReceiverName, string subject, string contentBody, string host, System.Boolean isBodyHtml)
{
TimeOut
= timeOut;
Encoding
= encoding;
MailFrom
= mailFrom;
UserName
= userName;
Pwd
= pwd;
DisplaySenderName
= displaySenderName;
MailTo
= mailTo;
DisplayReceiverName
= displayReceiverName;
Subject
= subject;
ContentBody
= contentBody;
Host
= host;
IsBodyHtml
= isBodyHtml;
}

/// <summary>
/// Send mail
/// </summary>
/// <param name="mailEntity">MailEntity object</param>
/// <returns></returns>
public bool SendMail(MailEntity mailEntity)
{
try
{
Encoding encoding
= mailEntity.Encoding;
MailMessage mailMessage
= new MailMessage(new MailAddress(mailEntity.MailFrom, mailEntity.DisplaySenderName, encoding),
new MailAddress(mailEntity.MailTo, mailEntity.DisplayReceiverName, encoding));
mailMessage.SubjectEncoding
= encoding;
mailMessage.Subject
= mailEntity.Subject;
mailMessage.IsBodyHtml
= mailEntity.IsBodyHtml;
mailMessage.Body
= mailEntity.ContentBody;
SmtpClient smtpClient
= new SmtpClient(mailEntity.Host);
smtpClient.Credentials
= new NetworkCredential(mailEntity.UserName, mailEntity.Pwd);
smtpClient.Timeout
= mailEntity.TimeOut;
smtpClient.Send(mailMessage);
return true;
}
catch
{
return false;
}
}

#region Properties

/// <summary>
/// the email send timeout
/// </summary>
public Int32 TimeOut { set; get; }
/// <summary>
/// the email address that will send the email
/// </summary>
public String MailFrom { set; get; }
/// <summary>
/// the email address that will receive the email
/// </summary>
public String MailTo { set; get; }
/// <summary>
/// the Encoding type that will edcode the email content
/// </summary>
public Encoding Encoding { set; get; }
/// <summary>
/// the name of the user who send the email
/// </summary>
public String UserName { set; get; }
/// <summary>
/// the password of the user who send the email
/// </summary>
public string Pwd { get; set; }
/// <summary>
/// the sender name that will display in the sender item
/// </summary>
public String DisplaySenderName { set; get; }
/// <summary>
/// the receiver name that will display in the receiver item
/// </summary>
public String DisplayReceiverName { set; get; }
/// <summary>
/// mail content
/// </summary>
public String ContentBody { set; get; }
/// <summary>
/// mail subject
/// </summary>
public String Subject { set; get; }
/// <summary>
/// mail host address
/// </summary>
public String Host { set; get; }
/// <summary>
/// if the mail body is in html format
/// </summary>
public Boolean IsBodyHtml { set; get; }
#endregion
}
}
上不了岸的鱼 | 老鸟四级 |园豆:4613 | 2009-01-01 15:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册