我有很多年的工作代码,但是从2个月开始在Windows 10中出现了错误(在Windows 10中工作了几个月,然后在同一台PC上工作了2个月)。我曾经创建过图像并将其保存在d:\中,然后将其作为附件发送到电子邮件中。从2个月开始,电子邮件停止发送图像文件,我注意到保存在硬盘上的图像,当SmtpServer1.Send(mail1);
被激活时,现在我以非常愚蠢的方式解决了问题,我首先向我自己发送空电子邮件,然后保存图像,然后我发送带有附件的所需电子邮件,以及2个月后发生的事情。是windows更新还是安全还是什么?我使用Windows 10在许多PC上测试了该程序并更改了图像位置,但我得到了相同的结果。请一些人帮助我并发送代码以强制保存创建的图像。
我的代码是:
var webBrowser = (WebBrowser)sender;
using( Bitmap bitmap = new Bitmap( 800, webBrowser.Height ) )
{
webBrowser.DrawToBitmap( bitmap, new Rectangle(0, 0, 800, bitmap.Height ) );
bitmap.Save( @"d:\wisetemp\wisesoftware.jpg", ImageFormat.Jpeg );
//next 2 lines solved the problem by forcing the file to be saved , thanks to M.Hassan
var stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if( File.Exists( @"d:\wisetemp\wisesoftware.jpg" ) )
mail.Attachments.Add( new Attachment( @"d:\wisetemp\wisesoftware.jpg" ) );
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential( Form1.sender_email, Form1.seder_email_pass );
smtpServer.EnableSsl = true;
smtpServer.Send( mail );
答案 0 :(得分:-1)
修改您的代码并抛出异常(如果没有发送以跟踪问题的根源
) if( File.Exists( @"d:\wisetemp\wisesoftware.jpg" ) )
{
mail.Attachments.Add( new Attachment( @"d:\wisetemp\wisesoftware.jpg" ));
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential( Form1.sender_email, Form1.seder_email_pass );
smtpServer.EnableSsl = true;
smtpServer.Send( mail );
}
else
{
//print log information about image size ,location , email send status to help fix problem
throw Exception ("somethong wrong ...");
}
修改强>
我使用了您的代码并完成了缺失的变量,并且它正常工作。 你能试试吗? 我认为问题可能在于从浏览器创建图像。我设置浏览器高度
public void Test1(string fromEmailAddress, string password)
{
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
WebBrowser webBrowser = new WebBrowser();
webBrowser.Height = 100;
var fname = @"wisesoftware.jpg";
try
{
using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
{
webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
bitmap.Save(fname, ImageFormat.Jpeg);
}
Attachment attachment = new Attachment(fname, MediaTypeNames.Application.Octet);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmailAddress);
mail.To.Add(fromEmailAddress);
mail.Subject = "Test send image";
mail.Body = "mail with image attachment";
mail.Attachments.Add(attachment);
if (File.Exists(fname))
{
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
编辑2:
将图像作为流发送
public void Test2(string fromEmailAddress, string password)
{
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
WebBrowser webBrowser = new WebBrowser();
webBrowser.Height = 100;
var fname = @"wisesoftware.jpg";
try
{
var stream = new MemoryStream();
using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
{
webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
bitmap.Save(fname, ImageFormat.Jpeg);
//saving the image to a MemoryStream, and then providing that MemoryStream to the attachment constructor:
bitmap.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
}
Attachment attachment = new Attachment(fname, MediaTypeNames.Application.Octet);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmailAddress);
mail.To.Add(fromEmailAddress);
mail.Subject = "Test send image";
mail.Body = "mail with image attachment";
mail.Attachments.Add(new Attachment(stream, "image/jpg"));
// mail.Attachments.Add(attachment);
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
编辑3:
//saving image as stream , send email with stream online and finally save stream to disk
public void Test3(string fromEmailAddress, string password)
{
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
WebBrowser webBrowser = new WebBrowser();
webBrowser.Height = 100;
var fname = @"wisesoftware.jpg";
try
{
var stream = new MemoryStream();
using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
{
webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
//saving the image to a MemoryStream, and then providing that MemoryStream to the attachment constructor:
bitmap.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
}
//add stream with defined friendly filename and mediaType as attachment
Attachment attachment = new Attachment(stream, fname, MediaTypeNames.Image.Jpeg);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmailAddress);
mail.To.Add(fromEmailAddress);
mail.Subject = "Test send image";
mail.Body = "mail with image attachment";
mail.Attachments.Add(attachment);
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
//write stream to file , close stream
using (FileStream file = new FileStream(fname, FileMode.Create, FileAccess.Write))
{
stream.Position = 0;
stream.WriteTo(file);
stream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}