c# 傳送郵件程式碼,帶附件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
namespace Test28
{
class Program
{
static void Main(string[] args)
{
Email email = new Email()
{
fromEmail = "傳送人郵箱",
fromPerson = "發件人",
toEmail = "接收人郵箱",
toPerson = "接收人",
encoding = "UTF-8",
smtpServer = "選用的郵件伺服器平【比如:mail.163.com】",
userName = "你郵箱的使用者名稱【比如:xxxxx@163.com】",
passWord = "你的郵箱的密碼",
emailTitle = "郵件標題",
emailContent = "郵件內容"
};
SendEmail(email);
}
#region 郵件傳送程式碼
/// <summary>
/// 郵件傳送程式碼
/// </summary>
/// <param name="email"></param>
public static void SendEmail(Email email)
{
//try
//{
//設定發件人信箱,及顯示名字
MailAddress from = new MailAddress(email.fromEmail, email.fromPerson);
//設定收件人信箱,及顯示名字
MailAddress to = new MailAddress(email.toEmail, email.toPerson);
//建立一個MailMessage物件
MailMessage oMail = new MailMessage(from, to);
oMail.Subject = email.emailTitle; //郵件標題
oMail.Body = email.emailContent; //郵件內容
oMail.IsBodyHtml = true; //指定郵件格式,支援HTML格式
System.Net.Mail.Attachment mailAttach_1 = new Attachment(@"c:\UserCenterLog.txt");//附件
oMail.Attachments.Add(mailAttach_1);
oMail.BodyEncoding = System.Text.Encoding.GetEncoding(email.encoding);//郵件採用的編碼
oMail.Priority = MailPriority.High;//設定郵件的優先順序為高
//傳送郵件伺服器
SmtpClient client = new SmtpClient();
client.Host = email.smtpServer; //指定郵件伺服器
client.Credentials = new NetworkCredential(email.userName, email.passWord);//指定伺服器郵件,及密碼
//傳送
client.Send(oMail); //傳送郵件
oMail.Dispose(); //釋放資源
//}
//catch(Exception ex)
//{
// StreamWriter writer = File.AppendText(@"c:\00.txt");
// writer.WriteLine(ex.Message);
// writer.Close();
// writer.Dispose();
//}
//finally
//{
//}
}
#endregion
}
class Email
{
public string fromEmail { get; set; }
public string fromPerson { get; set; }
public string toEmail { get; set; }
public string toPerson { get; set; }
public string encoding { get; set; }
public string smtpServer { get; set; }
public string userName { get; set; }
public string passWord { get; set; }
public string emailTitle { get; set; }
public string emailContent { get; set; }
}
}
相關文章
- Python 傳送帶有附件的郵件Python
- 如何使用 ABAP 程式碼傳送帶有 PDF 附件的電子郵件
- 【python】用SMTP模組傳送帶附件的郵件Python
- Python2.7 smtplib傳送帶附件郵件報錯STARTTLS解PythonTLS
- 如何使用ABAP傳送帶有PDF格式附件的電子郵件
- C# 傳送電子郵件原始碼片段C#原始碼
- 傳送帶圖片的郵件
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- 郵件傳送
- 傳送郵件
- iOS12系統應用傳送郵件中的附件iOS
- 谷歌郵箱,配置傳送郵件密碼谷歌密碼
- laravel 傳送郵件修改密碼Laravel密碼
- java郵件傳送Java
- python傳送郵件Python
- Django——郵件傳送Django
- gmail傳送郵件AI
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel 傳送郵件Laravel
- thinkjs 傳送郵件JS
- SpringBoot傳送郵件Spring Boot
- SpringBoot郵件傳送Spring Boot
- Laravel傳送郵件Laravel
- PHP傳送郵件PHP
- nodejs 傳送郵件NodeJS
- 郵件傳送,附件太大怎麼辦 → 那就用分卷壓縮吧
- 在Linux命令列傳送電子郵件附件的兩種方法Linux命令列
- Laravel 重置密碼傳送郵件分析Laravel密碼
- python: 傳送內容帶圖片的郵件Python
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- JavaMail 郵件傳送,有意思的附件名亂碼 → 客戶端正常,web端亂碼JavaAI客戶端Web
- python 壓縮檔案並進行郵件傳送(附件格式為zip)Python
- golang傳送郵件(qq郵箱)Golang
- Android 快速傳送郵件Android
- 使用phpmailer傳送郵件PHPAI
- SpringBoot傳送郵件(二)Spring Boot
- SpringBoot傳送郵件(三)Spring Boot
- 定時傳送郵件