我认为我们可以创建一个文本图像并通过执行这样的代码将其转换为jpeg文件,但是如何使用mail和send.any帮助嵌入此图像
string Text = HttpContext.Current.Request.QueryString["Text"];
Color FontColor = Color.Blue;
Color BackColor = Color.White;
String FontName = "Times New Roman";
int FontSize = 10;
int Height = 150;
int Width = 150;
Bitmap bitmap = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray;
System.Drawing.Font font = new System.Drawing.Font(FontName, FontSize);
PointF point = new PointF(5.0F, 5.0F);
SolidBrush BrushForeColor = new SolidBrush(FontColor);
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
System.Drawing.Rectangle displayRectangle = new System.Drawing.Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
graphics.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle);
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
graphics.DrawString(Text, font, Brushes.Red, (RectangleF)displayRectangle, format2);
HttpContext.Current.Response.ContentType = "image/jpeg";
bitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
答案 0 :(得分:2)
下面的代码示例跳过创建消息并添加主题,正文和地址。它显示了用于嵌入最初存储在字节数组中的图像的代码。关键是将图像放入内存流中。
//... other System.Net.Mail.MailMessage creation code
// CustomerSignature is a byte array containing the image
System.IO.MemoryStream ms = new System.IO.MemoryStream(CustomerSignature);
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
mailMessage.Attachments.Add(imageAttachment);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(ms, "image/jpeg");
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);
我遇到过嵌入式图像在某些电子邮件程序中显示的问题,而不是其他问题。我修改了创建链接资源的行,创建新AlternativeView的行和图像现在可以在更广泛的程序中查看。