close


C#發送嵌入圖片的郵件
 
 
 
 
 

使用System.Net.Mail寄送方式

 

<%@ Page Language="C#" Debug="true"%>

 

<script runat="server">

    EcnSql.UseSql useSql = new EcnSql.UseSql(Project.GetSN()); string sql; string[] values = new string[5];

    protected void Page_Load(object sender, EventArgs e)

    {

string from_user = aaa@aaa.aaa.aaa;

string to_user = bbb@bbb.bbb.bbb;

string cc_user = "";

string bcc_user = "";

string subject = "測試主旨test";

string mailbody = "<h1>測試文字TEST</h1><br><img src='cid:attach.gif'></img>";

string abs_path_filename = "d:\\test1.jpg|d:\\test2.jpg";

sendmail(from_user, to_user, cc_user, bcc_user, subject, mailbody, abs_path_filename);

    }

 

public string sendmail(string aaa,string bbb,string ccc,string ddd,string eee,string fff,string ggg)  //aaa 為from, bbb為to, ccc為cc, ddd為bcc, eee為subject, fff為body, ggg為attach file實體路徑 多檔以|分隔  

{

string FromAddress = aaa;

string ToAddress = bbb;

string CcAddress = ccc;

string BccAddress = ddd;

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(FromAddress, ToAddress);

if (CcAddress != "")

mm.CC.Add(CcAddress);

if (BccAddress != "")

mm.Bcc.Add(BccAddress);

mm.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");

mm.SubjectEncoding = System.Text.Encoding.GetEncoding("utf-8");

 

mm.Subject = eee;

 

string strFilePath = "d:\\attach.gif";   //html mail要嵌入的圖檔

 

System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);

attachment1.Name = System.IO.Path.GetFileName(strFilePath);

attachment1.NameEncoding = System.Text.Encoding.GetEncoding("utf-8");

attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;           

attachment1.ContentDisposition.Inline = true;

attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;

string cid = attachment1.ContentId;  //此處取得嵌入圖檔cid

mm.Attachments.Add(attachment1);

 

System.Net.Mail.Attachment attachment;

if (ggg != "")

{

string [] m_attach = ggg.Split('|');

for (int i = 0; i < m_attach.Length; i++)

{

attachment = new System.Net.Mail.Attachment(m_attach[i].ToString());

mm.Attachments.Add(attachment);

}

}

           

mm.Body = fff.Replace("cid:attach.gif", "cid:" + cid);   //html mail使用<img src='cid:cid值'>

mm.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

smtp.Host = "192.168.1.1";

smtp.UseDefaultCredentials = false;

try

{

smtp.Send(mm);

return "OK";

}

catch(System.Net.Mail.SmtpException se)

{

return se.ToString();

}

}

</script>

 

<html>

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body bgcolor="#ffffff">

    <form id="form1" runat="server">

<asp:Label ID="Label99" runat="server" Text=""></asp:Label>

    </form>

</body>

</html>



arrow
arrow
    全站熱搜

    系統家具工廠 發表在 痞客邦 留言(0) 人氣()