C#編程實(shí)現(xiàn)發(fā)送郵件的方法(可添加附件)
本文實(shí)例講述了C#編程實(shí)現(xiàn)發(fā)送郵件的方法。分享給大家供大家參考,具體如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsFormsApplication63
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//打開上傳附件的對(duì)話框
private void btnUP_Click(object sender, EventArgs e)
{
oFDialogSFile.InitialDirectory = "C:\\";//設(shè)置對(duì)話框的初始目錄為C盤
oFDialogSFile.Filter = "all files (*.*)|*.*";//篩選字符串為所有文件
oFDialogSFile.RestoreDirectory = true;
oFDialogSFile.ShowDialog();
cboxAccessories.Items.Add(oFDialogSFile.FileName.Trim());//當(dāng)選擇好文件后將文件名賦值給下拉框
}
//發(fā)送郵件
private void btnSend_Click(object sender, EventArgs e)
{
try
{
string file = Application.StartupPath + "testXML.xml";
//SmtpClient下的一個(gè)對(duì)象,用以設(shè)置郵件的主題和內(nèi)容
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
//發(fā)送端到接收端的郵箱地址
myMail = new System.Net.Mail.MailMessage(txtSEmail.Text.Trim(), txtCEmail.Text.Trim());
myMail.Subject = txtETitle.Text.Trim();
myMail.Body = txtEContent.Text.Trim();
if (cboxAccessories.Items.Count > 0)
{
for (int i = 0; i < cboxAccessories.Items.Count; i++)
{ //建立郵件附件類的一個(gè)對(duì)象,語法格式為System.Net.Mail.Attachment(文件名,文件格式)
System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(
cboxAccessories.Items[i].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
//MIME協(xié)議下的一個(gè)對(duì)象,用以設(shè)置附件的創(chuàng)建時(shí)間,修改時(shí)間以及讀取時(shí)間
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
//用smtpclient對(duì)象里attachments屬性,添加上面設(shè)置好的myattachment
myMail.Attachments.Add(myAttachment);
}
}
//建立發(fā)送對(duì)象client,驗(yàn)證郵件服務(wù)器,服務(wù)器端口,用戶名,以及密碼
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(txtSService.Text.Trim(), Convert.ToInt32(txtServicePort.Text.Trim()));
client.Credentials = new System.Net.NetworkCredential(txtUPwd.Text.Trim(), txtCEmail.Text.Trim());
client.Send(myMail);
MessageBox.Show("郵件發(fā)送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void 刪除上傳文件_Click(object sender, EventArgs e)
{
if (cboxAccessories.Text == "")
{
MessageBox.Show("沒有附件可刪!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
cboxAccessories.Items.Remove(cboxAccessories.Text.Trim());
}
}
}
}
效果圖如下:

點(diǎn)擊發(fā)送,效果如下

總結(jié)一下,發(fā)送郵件的過程如下:
1.建立System.Net.Mail.MailMessage下的對(duì)象,設(shè)置郵件的內(nèi)容和主題。
2.如果要添加附件,建立System.Net.Mail.Attatch下的對(duì)象,用1中的對(duì)象中的添加附件的屬性讀取它。
3.SEND郵件(MAIL+附件)。
如果要實(shí)現(xiàn)群發(fā),則可以將發(fā)送郵件的代碼(TRY附近的幾行)寫成方法,循環(huán)調(diào)用即可。
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
WPF使用WrapPanel實(shí)現(xiàn)虛擬化效果
這篇文章主要為大家詳細(xì)介紹了如何利用WPF WrapPanel實(shí)現(xiàn)虛擬化效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09
C#中結(jié)構(gòu)體定義并轉(zhuǎn)換字節(jié)數(shù)組詳解
在寫C#TCP通信程序時(shí),發(fā)送數(shù)據(jù)時(shí),只能發(fā)送byte數(shù)組,處理起來比較麻煩不說,如果是和VC6.0等寫的程序通信的話,很多的都是傳送結(jié)構(gòu)體,在VC6.0中可以很方便的把一個(gè)char[]數(shù)組轉(zhuǎn)換為一個(gè)結(jié)構(gòu)體,而在C#卻不能直接把byte數(shù)組轉(zhuǎn)換為結(jié)構(gòu)體,要在C#中發(fā)送結(jié)構(gòu)體,應(yīng)該怎么做呢?2017-11-11
C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡潔實(shí)用,需要的朋友可以參考下2016-06-06
C# 使用CancellationTokenSource取消多線程
有時(shí)間我們?cè)谑褂枚嗑€程的時(shí)候,需要取消線程的執(zhí)行,可以使用CancellationTokenSource來取消對(duì)Task開辟多線程的取消,感興趣的可以了解一下2021-08-08
基于C#實(shí)現(xiàn)Windows服務(wù)狀態(tài)啟動(dòng)和停止服務(wù)的方法
這篇文章主要介紹了基于C#實(shí)現(xiàn)Windows服務(wù)狀態(tài)啟動(dòng)和停止服務(wù)的方法,詳細(xì)講述了實(shí)現(xiàn)這一功能的具體步驟,代碼簡潔易懂,需要的朋友可以參考下2014-09-09
運(yùn)用示例簡單講解C#取消令牌CancellationTokenSource
這篇文章運(yùn)用示例簡單講解C#取消令牌CancellationTokenSource,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C#控制臺(tái)輸出進(jìn)度和百分比的實(shí)例代碼
C#控制臺(tái)輸出進(jìn)度和百分比的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
C#實(shí)現(xiàn)基于ffmpeg加虹軟的人臉識(shí)別的示例
本篇文章主要介紹了C#實(shí)現(xiàn)基于ffmpeg加虹軟的人臉識(shí)別的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常詳解
這篇文章主要給大家介紹了關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04

