我用MailMessage类写了以下代码,想实现邮件的发送,但总是出现错误:“邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨ ”请高手指点。
string file = Server.MapPath("testXML.xml");
MailMessage myMail = new System.Net.Mail.MailMessage();
myMail = new System.Net.Mail.MailMessage(txtSender.Text.Trim(), txtReceiver.Text.Trim(),txtSubject.Text.Trim(),txtContent.Text.Trim());
if (ddlAccessories.Items.Count > 0)
{
for (int i = 0; i < ddlAccessories.Items.Count; i++)
{
System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(Server.MapPath(ddlAccessories.Items[i].Value), System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
myMail.Attachments.Add(myAttachment);
}
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
client.Credentials = new System.Net.NetworkCredential("[email protected]", "zs*875158");
client.Send(myMail);
string file = Server.MapPath("testXML.xml");
MailMessage myMail = new System.Net.Mail.MailMessage();
myMail = new System.Net.Mail.MailMessage(txtSender.Text.Trim(), txtReceiver.Text.Trim(),txtSubject.Text.Trim(),txtContent.Text.Trim());
if (ddlAccessories.Items.Count > 0)
{
for (int i = 0; i < ddlAccessories.Items.Count; i++)
{
System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(Server.MapPath(ddlAccessories.Items[i].Value), System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
myMail.Attachments.Add(myAttachment);
}
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
client.Credentials = new System.Net.NetworkCredential("[email protected]", "zs*875158");
client.Send(myMail);
解决方案 »
- 各位高手们谁能提供一份完整的session超时代码啊?实在是搞不懂了,就是验证不出来session超时啊!
- 请问兄弟们此错误应该如何解决
- 做一个网站 大家来帮我估计下时间
- bcp导入大量数据的问题,出现错误,请高手指点,谢谢!
- Enterprise Library for .NET Framework 2.0 如何使用?下载地址?最好是中文,谢谢,来者有分
- 各位大虾:小弟最近遇到了点工程上的难题,不知道那位能帮小弟个忙改一下;把邮箱发给我,我把程序发过去;跪求!!急急急!!!
- datagrid帮定dataset的时候,如何动态操作图片?
- 急、急、急,dbf的数据库文件如何用asp.ent建立连接?
- Request.UserLanguages获取值问题
- 如何统计DataGrid中某列的数据呢?(要求不一样)
- 在做交行网银接口时提示"验证签名数据失败"
- 请问SOAOffice NTKO、iWebOffice三种有什么不同?
using System.Net.Mail;
using System.Net.Mime;
using System.Net; public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = @"D:\asdf.txt";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"[email protected]",
"[email protected]",
"test",
"no du"); // Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Send(message);
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
} protected void Timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Second % 20 == 0)
{
CreateMessageWithAttachment("smtp.163.com");
}
}
{ SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
smtp.Host = "smtp.163.com"; //指定 smtp 服务器地址
smtp.Port = 25; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
//如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
smtp.UseDefaultCredentials = true;
//如果需要认证,则用下面的方式
smtp.Credentials = new NetworkCredential("[email protected]", "****");
MailMessage mm = new MailMessage(); //实例化一个邮件类
mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
mm.From = new MailAddress("[email protected]", "真有意思测试", Encoding.GetEncoding(936));
//收件方看到的邮件来源;
//第一个参数是发信人邮件地址
//第二参数是发信人显示的名称
//第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码
//936是简体中文的codepage值
//注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败
mm.ReplyTo = new MailAddress("[email protected]", "我的接收邮箱", Encoding.GetEncoding(936));
//ReplyTo 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信
//上面后两个参数的意义, 同 From 的意义
//mm.CC.Add("[email protected],[email protected],[email protected]");
//邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号 分开 //当然也可以用全地址,如下:
mm.CC.Add(new MailAddress("[email protected]", "抄送者A", Encoding.GetEncoding(936)));
//mm.CC.Add(new MailAddress("[email protected]", "抄送者B", Encoding.GetEncoding(936)));
//mm.CC.Add(new MailAddress("[email protected]", "抄送者C", Encoding.GetEncoding(936))); //mm.Bcc.Add("[email protected],[email protected]");
//邮件的密送者,支持群发,多个邮件地址之间用 半角逗号 分开 //当然也可以用全地址,如下:
//mm.CC.Add(new MailAddress("[email protected]", "密送者D", Encoding.GetEncoding(936)));
//mm.CC.Add(new MailAddress("[email protected]", "密送者E", Encoding.GetEncoding(936)));
//mm.Sender = new MailAddress("[email protected]", "邮件发送者", Encoding.GetEncoding(936));
//可以任意设置,此信息包含在邮件头中,但并不会验证有效性,也不会显示给收件人
//说实话,我不知道有啥实际作用,大家可不理会,也可不写此项
//mm.To.Add("[email protected],[email protected]");
//邮件的接收者,支持群发,多个地址之间用 半角逗号 分开 //当然也可以用全地址添加 mm.To.Add(new MailAddress("[email protected]", "接收者g", Encoding.GetEncoding(936)));
//mm.To.Add(new MailAddress("[email protected]", "接收者h", Encoding.GetEncoding(936)));
mm.Subject = "这是邮件标题-测试"; //邮件标题
mm.SubjectEncoding = Encoding.GetEncoding(936);
// 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
// 936是简体中文的pagecode,如果是英文标题,这句可以忽略不用
mm.IsBodyHtml = true; //邮件正文是否是HTML格式 mm.BodyEncoding = Encoding.GetEncoding(936);
//邮件正文的编码, 设置不正确, 接收者会收到乱码 mm.Body = "<font color=\"red\">邮件测试,呵呵</font>";
//邮件正文
mm.Attachments.Add(new Attachment(@"c:\d\1.doc", System.Net.Mime.MediaTypeNames.Application.Rtf));
//添加附件,第二个参数,表示附件的文件类型,可以不用指定
//可以添加多个附件
//mm.Attachments.Add(new Attachment(@"d:b.doc"));
smtp.Send(mm); //发送邮件,如果不返回异常, 则大功告成了。
}我参照这种方法做也碰到同样的问题。
不解
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
/// <summary>
/// 发邮件模块
/// Author:tonyepaper.cnblogs.com
/// </summary>
public class Mail
{
private string senderAddress;
/// <summary>
/// 发件人
/// </summary>
public string SenderAddress
{
get { return senderAddress; }
set { senderAddress = value; }
}
private string receiverAddess;
/// <summary>
/// 收件人
/// </summary>
public string ReceiverAddess
{
get { return receiverAddess; }
set { receiverAddess = value; }
}
private string subject;
/// <summary>
/// 主题
/// </summary>
public string Subject
{
get { return subject; }
set { subject = value; }
}
private string body;
/// <summary>
/// 内容
/// </summary>
public string Body
{
get { return body; }
set { body = value; }
}
private string smtpHost;
/// <summary>
/// SMTP主机
/// </summary>
public string SmtpHost
{
get { return smtpHost; }
set { smtpHost = value; }
}
private int smtpPort;
/// <summary>
/// SMTP端口
/// </summary>
public int SmtpPort
{
get { return smtpPort; }
set { smtpPort = value; }
}
private string smtpPassword;
/// <summary>
/// SMTP密码
/// </summary>
public string Password
{
get { return smtpPassword; }
set { this.smtpPassword = value; }
}
/// <summary>
/// 初始化发件人
/// </summary>
public Mail()
{
senderAddress = "[email protected]";
smtpPassword ="*****";
smtpHost = "smtp.gmail.com";
smtpPort = 587;
}
/// <summary>
/// 邮件
/// </summary>
/// <param name="receiverAddess">收件人地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
public Mail(string receiverAddess, string subject, string body)
: this()
{
this.receiverAddess = receiverAddess;
this.subject = subject;
this.body = body;
}
/// <summary>
/// 发送邮件
/// </summary>
public bool Send()
{
MailMessage mailMessage = new MailMessage(senderAddress, receiverAddess);
mailMessage.Subject = subject;
mailMessage.Body = body; SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort);
//使用SSL加密连线
smtpClient.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(senderAddress, smtpPassword);
smtpClient.Credentials = networkCredential;
try
{
smtpClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
调用protected void Button1_Click(object sender, EventArgs e)
{
Mail mail = new Mail("[email protected]", "aaaa", "aaaaaa");//初始化收件人
mail.Send();
}
http://blog.csdn.net/ericawangrong/archive/2009/04/16/4083097.aspx
看看吧,写的很详细
<img src ="http://hiphotos.baidu.com/%B7%C9de%CE%DD/abpic/item/08a805ab63b6ecd01f17a2c9.jpg"/>
Failure sending mail.