vs2003发送邮件异常

loveflydhl 2009-05-04 03:56:30
The transport failed to connect to the server.
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Runtime.InteropServices.COMException: The transport failed to connect to the server.

[TargetInvocationException: 调用的目标发生了异常。]
[HttpException (0x80004005): 未能访问“CDO.Message”对象。]


代码:

private void Btn1_Click(object sender, System.EventArgs e)
{
sendEamil(txt_add.Text,"dhloo@126.com",txt_title.Text,txt_content.Text,"dhloo@126.com","***","123.125,50.22","");
}
public void sendEamil(string toAddress,string formAddress,string maintile,string body,string EmailUserName,string EmailPWD,string EmailServerIP,string MailAttachmentUrl)//发送邮件
{
MailMessage mail = new MailMessage();
mail.To =toAddress;
mail.From =formAddress;
mail.Subject =maintile;
mail.Body = body;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",EmailUserName); //用户名全名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", EmailPWD); //密码全名




if(MailAttachmentUrl!="")
{
MailAttachment mailAttach= new MailAttachment(MailAttachmentUrl); //附件
mail.Attachments.Add(mailAttach);
}
SmtpMail.SmtpServer =EmailServerIP; //smtp邮件服务器


SmtpMail.Send(mail);


}


...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveflydhl 2009-05-05
  • 打赏
  • 举报
回复
看样没人知道了
loveflydhl 2009-05-04
  • 打赏
  • 举报
回复
回复楼上 Vs2003 都没得System.net.maile类 发帖子就说了是VS2003
qiqundelang 2009-05-04
  • 打赏
  • 举报
回复
没遇到过
llsen 2009-05-04
  • 打赏
  • 举报
回复
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(
"asdf@163.com",
"asdf@163.com",
"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");
}
}


看这个
蓝海D鱼 2009-05-04
  • 打赏
  • 举报
回复
.net,利用STMP服务器发送邮件时报错。

造成这种原因可能有很多种,网上也有很多相关的文章。如果你确定你之前发送邮件成功,或者在本机成功,而发布到服务器上时出问题。可以考虑看看以下原因:

1. 是否因为防火墙和杀毒软件导致的,他们均带有邮件拦截的功能。
2. 是否因为没有注册相关动态链接库文件,使用命令:regsvr32 cdonts.dll试试(使用该命令,需要先进入system32目录下,有可能会没有cdonts.dll文件,没有就去网上下一个)。
3. STMP服务是否安装,控制面板->添加删除程序->添加windows组件->IIS
4. 邮件服务器是否正确(有时候是权限问题,换个邮件服务器试试)

资料
资料

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧