我在附加文件发送邮件时得到了这样的错误 [英] i got the error like this while attach the file to send a mail
本文介绍了我在附加文件发送邮件时得到了这样的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到这样的错误,同时附上文件发送邮件但没有附件邮件已发送(如果我隐藏附加评论)
错误如下:
-----------------------
i got the error like this while attach the file to send a mail but without attachment the message has been send (if i hide attach comment)
the error like this:
-----------------------
System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\free-photo-background-952-m.jpg'. File name: 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\free-photo-background-952-m.jpg' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) at System.Net.Mail.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName) at customerdetails.SendEmail(String toAddress, String ccAddress, String bccAddress, String subject, String body, MailPriority priority, Boolean isHtml) in e:\E-mobileshop\admin\customerdetails.aspx.cs:line 275
---------------------- ------- --------
和我使用此代码:
-------------------------------------
and i use this code :
protected void SendEmailWithAttachment(object sender, EventArgs e)
{
SendEmail(txtTo.Text.Trim(), "", "", txtSubject.Text.Trim(), txtBody.Text.Trim(),System.Net.Mail.MailPriority.High, false);
}
private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, System.Net.Mail.MailPriority priority, bool isHtml)
{
body = txtBody.Text.ToString();
subject = txtSubject.Text.ToString();
toAddress = txtTo.Text.ToString();
try
{
SmtpClient smtpClient = new SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
MailAddress fromAddress = new MailAddress("ramji.kid@gmail.com", "ramji.kid, gmail.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.gmail.com"; //you can also specify mail server IP address here
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential("ramji.kid@gmail.com", "hi");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = fromAddress;
message.Priority = priority;
// To address collection of MailAddress
message.To.Add(toAddress);
message.Subject = subject;
if (ccAddress.Length > 0)
{
message.CC.Add(ccAddress);
}
if (bccAddress.Length > 0)
{
message.Bcc.Add(bccAddress);
}
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = body;
//Add the attachment, if any
if (FileUpload1.PostedFile.ContentLength > 0)
{
Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
message.Attachments.Add(attachment);
}
// Send SMTP mail
smtpClient.Send(message);
lblMessage.Text = "Email sent to " + toAddress + " successfully !";
}
catch (Exception ee)
{
lblMessage.Text = ee.ToString();
}
}
推荐答案
您可能希望将代码更新为以下内容;
You may want to update your code to the following;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Attachment myAttachment =
new Attachment(FileUpload1.FileContent, fileName
还使用调试器查看文件内容和路径是否为空。
祝你好运,
OI
Also use a debugger to see if the file content and path is not null.
Good luck,
OI
使用Server.MapPath
代替Path.GetFullPath()
。可能它会帮助你。
谢谢
UseServer.MapPath
in place ofPath.GetFullPath()
. May be it will help you.
Thanks
这篇关于我在附加文件发送邮件时得到了这样的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文