C# 发邮件类可发送附件
生活随笔
收集整理的這篇文章主要介紹了
C# 发邮件类可发送附件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;namespace Ajax.發(fā)郵件
{public class GetMail{//MailAddress ds = new MailAddress("1738819932@qq.com");// Send(ds, "674580075@qq.com", "郵件主題", "郵件內(nèi)容","附件名稱");public GetMail(){ }/// <summary>/// 發(fā)送電子郵件/// </summary>/// <param name="MessageFrom">發(fā)件人郵箱地址</param>/// <param name="MessageTo">收件人郵箱地址</param>/// <param name="MessageSubject">郵件主題</param>/// <param name="MessageBody">郵件內(nèi)容</param>/// <param name="SUpFile">附件</param>/// <returns></returns>public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody, string SUpFile){MailMessage message = new MailMessage();message.From = MessageFrom;message.To.Add(MessageTo); //收件人郵箱地址可以是多個(gè)以實(shí)現(xiàn)群發(fā)
message.Subject = MessageSubject;message.Body = MessageBody;if (SUpFile != ""){SUpFile = Server.MapPath("~/發(fā)郵件/Upfile/" + SUpFile);//獲得附件在本地地址//將文件進(jìn)行轉(zhuǎn)換成AttachmentsAttachment data = new Attachment(SUpFile, MediaTypeNames.Application.Octet);// Add time stamp information for the file.ContentDisposition disposition = data.ContentDisposition;disposition.CreationDate = System.IO.File.GetCreationTime(SUpFile);disposition.ModificationDate = System.IO.File.GetLastWriteTime(SUpFile);disposition.ReadDate = System.IO.File.GetLastAccessTime(SUpFile);message.Attachments.Add(data);System.Net.Mime.ContentType ctype=new System.Net.Mime.ContentType (); }message.IsBodyHtml = true; //是否為html格式message.Priority = MailPriority.Normal; //發(fā)送郵件的優(yōu)先等級(jí)SmtpClient sc = new SmtpClient();sc.Host = "smtp.qq.com"; //指定發(fā)送郵件的服務(wù)器地址或IPsc.Port = 25; //指定發(fā)送郵件端口sc.Credentials = new System.Net.NetworkCredential("發(fā)送郵箱賬號(hào)", "賬號(hào)密碼"); //指定登錄服務(wù)器的try{sc.Send(message); //發(fā)送郵件
}catch{return false;}return true;}}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiao-bei/p/3928959.html
總結(jié)
以上是生活随笔為你收集整理的C# 发邮件类可发送附件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 短程调度 中程调度 长程调度
- 下一篇: (软件工程复习核心重点)第十二章软件项目