本文介绍了发送带有图片附件的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人;
我有一个List realimages;
并且我想通过我拥有的图像发送电子邮件..
我已经编写了代码,但是可以附加文件,但是无法将图像流化为字节,然后通过邮件附加..
这是我的功能
Dear All;
I have an List realimages ;
and I want to send email by the images I have ..
I have wrote a code but it I can attach a file , but I cannot stream the image into bytes then attach it by the mail..
here is my function
public void button3_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage(
"[email protected]",
"[email protected]",
"Quarterly data report.",
"See the attached spreadsheet.");
MemoryStream stream = new MemoryStream();
realimage[0].Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
// MemoryStream ms = new MemoryStream();
// StreamWriter sw = new StreamWriter(stream);
//sw.Write();
// sw.Flush();
stream.ToArray();
Attachment data = null;
MessageBox.Show(stream.Length.ToString());
byte[] contentAsBytes;
// FileData holds byte[] that is the contents of the file
contentAsBytes = stream.ToArray();
stream.Write(contentAsBytes, 0, contentAsBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
// content type for file info
ContentType contentType = new ContentType();
contentType.MediaType = MediaTypeNames.Application.Octet;
contentType.Name = "test";
ContentType contenttype = new ContentType();
contenttype.MediaType = MediaTypeNames.Text.Html;
// Create the file attachment for this e-mail message.
data = new Attachment(stream, contenttype);
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient("smtp.gmail.com");
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString());
}
// 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();
}
如何通过缓冲没有文件路径的图像来发送电子邮件并附加在邮件中..
问候...
How can i send an email by buffering the images without a file path and attach in the mail..
regards...
推荐答案
这篇关于发送带有图片附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!