Lumisoft.NET SMTP邮件发送

.Net技术 码拜 10年前 (2014-11-13) 4925次浏览 0个评论

Lumisoft.NET是个非常优秀的开源组件,用该组件来设计开发邮件工具,方便快捷。本文为使用该组件实现邮件的发送功能代码

核心代码如下:

#region method CreateMessage

/// <summary>
/// Creates Mime message based on UI data.
/// </summary>
private Mail_Message CreateMessage()
{
Mail_Message m = new Mail_Message();
m.MimeVersion = “1.0”;
m.Date = DateTime.Now;
m.MessageID = MIME_Utils.CreateMessageID();
m.From = Mail_t_MailboxList.Parse(m_pFrom.Text);
m.To = Mail_t_AddressList.Parse(m_pTo.Text);
m.Subject = m_pSubject.Text;
//if(m_pRequestRead.Checked && m_pFrom.Text.Length > 0){
// m.DispositionNotificationTo = m_pFrom.Text;
//}

if(m_pAttachments.Items.Count > 0){
//— multipart/mixed ————————————————————————————————-
MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace(‘-‘,’.’);
MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
m.Body = multipartMixed;

//— multipart/alternative —————————————————————————————–
MIME_Entity entity_mulipart_alternative = new MIME_Entity();
MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace(‘-‘,’.’);
MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
entity_mulipart_alternative.Body = multipartAlternative;
multipartMixed.BodyParts.Add(entity_mulipart_alternative);

//— text/plain —————————————————————————————————-
MIME_Entity entity_text_plain = new MIME_Entity();
MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
entity_text_plain.Body = text_plain;
text_plain.SetText(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,m_pText.Text);
multipartAlternative.BodyParts.Add(entity_text_plain);

//— text/html ——————————————————————————————————
MIME_Entity entity_text_html = new MIME_Entity();
MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
entity_text_html.Body = text_html;
text_html.SetText(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,RtfToHtml());
multipartAlternative.BodyParts.Add(entity_text_html);

// Create attachment etities: — applactation/octet-stream ————————-
foreach(ListViewItem item in m_pAttachments.Items){
multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(item.Tag.ToString()));
}
}
else{
//— multipart/alternative —————————————————————————————–
MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace(‘-‘,’.’);
MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
m.Body = multipartAlternative;

//— text/plain —————————————————————————————————-
MIME_Entity entity_text_plain = new MIME_Entity();
MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
entity_text_plain.Body = text_plain;
text_plain.SetText(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,m_pText.Text);
multipartAlternative.BodyParts.Add(entity_text_plain);

//— text/html ——————————————————————————————————
MIME_Entity entity_text_html = new MIME_Entity();
MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
entity_text_html.Body = text_html;
text_html.SetText(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,RtfToHtml());
multipartAlternative.BodyParts.Add(entity_text_html);
}

return m;
}

#endregion

 

try{
Mail_Message message = CreateMessage();

if(m_pSendType.SelectedItem.ToString() == “Smart Host”){
SMTP_Client.QuickSendSmartHost(m_pSmartHost.Text,25,false,message);
}
else if(m_pSendType.SelectedItem.ToString() == “Direct with DNS”){
SMTP_Client.QuickSend(message);
}

MessageBox.Show(this,”Message sent sucessfully !”,”Info:”,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception x){
MessageBox.Show(this,”Error: ” + x.Message + “.”,”Error:”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Lumisoft.NET SMTP邮件发送
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!