在网上搜的代码会报错,无法发送邮件,
请哪高手试着向 zhangxinpingit@163.com zhangxinpingit@yahoo.cn zhangxinpingit@sina.com zhangxinpingit@gmail.com 这几个邮箱上发一下邮件,内容就是 密码:aaa 就可以了,请高手们测试通过了后在把代码贴出来,十万火急!
万分感谢!
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="strSmtpServer">smtp服务器地址或者域名</param>
/// <param name="strFrom">发件人</param>
/// <param name="strFromPass">发件人密码</param>
/// <param name="strto">收件人</param>
/// <param name="strSubject">邮件标题</param>
/// <param name="strBody">邮件内容</param>
/// <param name="filePath">附件路径</param>
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, string filePath)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
if (File.Exists(filePath))
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(filePath);
string fileName = Path.GetFileName(filePath);
attachment.Name = fileName;
message.Attachments.Add(attachment);
}
//message.BodyEncoding = System.Text.Encoding.Default;
//message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
message.IsBodyHtml = true;
client.Send(message);
}
我正在用的,因该没什么问题
一楼,貌似都是一个人发的
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
#region 邮件激活
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="mailNo">使用邮箱</param>
/// <param name="mailNoPwd">使用邮箱密码</param>
/// <param name="host">邮箱服务器</param>
/// <param name="Port">邮箱服务器端口</param>
/// <param name="mailto">用户邮箱</param>
/// <param name="subject">邮箱主题</param>
/// <param name="body">邮箱主体</param>
public static void sendMail(string mailNo, string mailNoPwd, string host, int Port, string mailto, string subject, string body)
{
string guidStr = Guid.NewGuid().ToString();
try
{
//string fromAddress = mailNo; //发送人
//string toAddress = mailto; //接收人
MailMessage mm = new MailMessage(mailNo, mailto); //建立邮件
mm.Subject = subject; //邮件主题
mm.Body = body; //邮件主体
mm.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = host;
client.Port = Port;
client.Credentials = new System.Net.NetworkCredential(mailNo, mailNoPwd);
client.Send(mm);
}
catch (Exception ex)
{
if (ex is SmtpException)
{
ex.ToString();
}
else
{
}
}
}
#endregion
使用:
SendMail.sendMail("xxxx@163.com", "密码", "mail.163.com", 25, Email, "职南针注册", http://www.u3w.com);
下面是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
}
}