[GCP]Mail Service

在GCP上建立發送MAIL服務,透過公司自家的SMTP Server

不能使用Standard Email Ports : 25

Port 25 is always blocked and cannot be used, even through SMTP relay using G Suite

整合方式:(本次跟IT合作,使用上面方式改587 port)

  • Use port 465, 587
  • GCP Third-party partners : SendGrid, Mailgun, and Mailjet

C# Code

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
//建立MailMessage物件
MailMessage mms = new MailMessage()
{
//指定一位寄信人MailAddress
From = new MailAddress(MailFrom),
//信件主旨
Subject = MailSub,
SubjectEncoding = Encoding.UTF8,
//信件內容(自刻HTML)
Body = getHtml(),

BodyEncoding = Encoding.UTF8,
//信件內容 是否採用Html格式
IsBodyHtml = true,
};

if (addresses != null)
{
foreach (string address in addresses)
{
//信件的收信人(們)address 密件
mms.Bcc.Add(new MailAddress(address));
}
}

using (SmtpClient client = new SmtpClient(SMTPServer, 587))
{
//設定你的帳號密碼
client.Credentials = new NetworkCredential(Id, Pwd);
//依需求開啟
//client.EnableSsl = true;
client.Send(mms);

//避免附件被Lock無法異動
if (mms.Attachments != null && mms.Attachments.Count > 0)
{
mms.Attachments.Dispose();
}
mms.Dispose();
}
-------------The End-------------