目前分類:程式設計 【 C# 】 (2)

瀏覽方式: 標題列表 簡短摘要
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
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;
using System.Net;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//發gmail需要 using System.Net; & using System.Net.Mail;
Send_Gmail(textBox1.Text, "測試主題", "收件人地址");
}


public void Send_Gmail(string msg, string mysubject, string address)
{
MailMessage mmsg = new MailMessage("寄件人地址", address);
mmsg.IsBodyHtml = true; //設定是否採用HTML格式
mmsg.BodyEncoding = Encoding.UTF8; //設定mail內容的編碼
mmsg.SubjectEncoding = Encoding.UTF8; //設定mail主旨的編碼
mmsg.Priority = MailPriority.Normal; //設定優先權 1.High 2.Normail 3.Low
mmsg.Subject = mysubject; // mail主旨
mmsg.Body = msg; //mail內容

SmtpClient MySmtp = new SmtpClient("smtp.gmail.com", 587); //允許程式使用smtp來發mail,並設定smtp server & port
MySmtp.Credentials = new NetworkCredential("帳號", "密碼"); //設定帳號與密碼 需要using system.net;
MySmtp.EnableSsl = true; //開啟SSL連線 (gmail體系須使用SSL連線)
MySmtp.Send(mmsg);

MySmtp = null; //將MySmtp清空
mmsg.Dispose(); //釋放資源

 

 

 




}
}
}

Domon 發表在 痞客邦 留言(0) 人氣()

Dear All,

 

Domon 發表在 痞客邦 留言(0) 人氣()