C#发送电子邮件与附加文件(图片) [英] C# Sending Email with attached file (image)
本文介绍了C#发送电子邮件与附加文件(图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的方法将使用SMTP中继服务器的电子邮件。
一切工作正常(电子邮件被发送),除外附加的文件(影像)是某种COM pressed / notexistent并不能从电子邮件检索。
的的方法是这样的:的
公共静态布尔SendEmail(HttpPostedFileBase uploadedImage)
{
尝试
{
VAR消息= MAILMESSAGE新()//为/发件人地址
{
主题=这是主题。
身体=这是文本。
}; 如果(uploadedImage =空&放大器;!&放大器; uploadedImage.ContentLength大于0)
{
System.Net.Mail.Attachment附件;
附件=新System.Net.Mail.Attachment(uploadedImage.InputStream,uploadedImage.FileName);
message.Attachments.Add(附后);
}
message.IsBodyHtml = TRUE; VAR smtpClient =新SmtpClient();
// SMTP凭证
smtpClient.Send(消息);
返回true;
}
赶上(异常前)
{
// LOGG例外
返回false;
}
}
- 的uploadedImage不为空。
- CONTENTLENGTH为1038946字节(正确的大小)。
然而,正在发送的电子邮件中包含的图像与正确的文件名附件,虽然它的大小为0字节。
我是什么失踪?
解决方案
@ChrisRun,
- 您应该更改参数HttpPostedFileBase作为例如字节[]。这样,您就可以重新使用在更多的地方你的类。
- 尝试改变文件名的ContentType并添加MediaTypeNames.Image.Jpeg。
-
此外,添加using指令进行处置和MAILMESSAGE SmtpClient
使用(VAR消息= MAILMESSAGE新
{
从=新的MailAddress(from@gmail.com),
主题=这是主题。
身体=这是文本。
IsBodyHtml = TRUE,
为了= {to@someDomain.com}
})
{
如果(镜像文件=空&放大器;!&放大器; imageFile.ContentLength大于0)
{
message.Attachments.Add(新附件(imageFile.InputStream,imageFile.ContentType,MediaTypeNames.Image.Jpeg));
} 使用(VAR的客户=新SmtpClient(smtp.gmail.com)
{
证书=新System.Net.NetworkCredential(用户,密码),
EnableSsl =真
})
{
client.Send(消息);
}
}
干杯
My method sends an email using a SMTP Relay Server.
Everything works fine (the email gets sent), except for that the attached file (the image) is somehow compressed/notexistent and not able to retrieve from the email.
The method looks like this:
public static bool SendEmail(HttpPostedFileBase uploadedImage)
{
try
{
var message = new MailMessage() //To/From address
{
Subject = "This is subject."
Body = "This is text."
};
if (uploadedImage != null && uploadedImage.ContentLength > 0)
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(uploadedImage.InputStream, uploadedImage.FileName);
message.Attachments.Add(attachment);
}
message.IsBodyHtml = true;
var smtpClient = new SmtpClient();
//SMTP Credentials
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
//Logg exception
return false;
}
}
- The uploadedImage is not null.
- ContentLength is 1038946 bytes (correct size).
However, the email that is being sent contains the image as an attachment with correct filename, although it's size is 0 bytes.
What am I missing?
解决方案
@ChrisRun,
- You should change the parameter HttpPostedFileBase as byte[] for example. This way you could re-use your class in more places.
- Try changing FileName for ContentType and add the MediaTypeNames.Image.Jpeg.
Also, add the using directive for dispose the MailMessage and SmtpClient
using (var message = new MailMessage { From = new MailAddress("from@gmail.com"), Subject = "This is subject.", Body = "This is text.", IsBodyHtml = true, To = { "to@someDomain.com" } }) { if (imageFile != null && imageFile.ContentLength > 0) { message.Attachments.Add(new Attachment(imageFile.InputStream, imageFile.ContentType, MediaTypeNames.Image.Jpeg)); } using (var client = new SmtpClient("smtp.gmail.com") { Credentials = new System.Net.NetworkCredential("user", "password"), EnableSsl = true }) { client.Send(message); } }
Cheers
这篇关于C#发送电子邮件与附加文件(图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文