大家好:
1.想請問使用gmail sever用SmtpClient.Send會停比較久winform才可以繼續使用
但如果用SmtpClient.SendAsync非同步方式會比較快,但是用outlook收信時,寄件人會變亂碼,想請問什麼方法可以修改嗎?
2.大量寄信的話是一封一封寄,還是多個收件者一次寄比較好呢?
謝謝大家
Mymail = new MailMessage();
Mymail.From = new MailAddress(Senderemail.ToString().Trim(),Sendname.ToString().Trim(),Encoding.UTF8); //寄件者email
Mymail.Subject = textBox4.Text;
Mymail.IsBodyHtml = true; //是否用html格式
foreach (var address in richTextBox1.Text.Split(new[] { ";","," }, StringSplitOptions.RemoveEmptyEntries))
{
if (IsValidEmail(address))
{
Mymail.To.Add(address);
}
}
SmtpServer.SendAsync(Mymail,"寄件中"); //有沒有記件成功好像還要再另寫程式..
SendAsync沒有比較快,只是不會被卡住視窗轉圈圈...
我都這樣做,outlook收到不會有亂碼,除非你的outlook是早期的express或live版,以下範例以SMTP需登錄的狀況
var stuff = HR.employee.Select(p=>p.Quit==null).ToArray().First();//多筆資料請以foreach處理
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "mail.abcdefg.com.tw";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("mis", "abc123456");
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("mis@abcdefg.com.tw","管理MIS",Encoding.GetEncoding("utf-8"));
message.Subject = "教育訓練通知";
message.SubjectEncoding = Encoding.GetEncoding("utf-8");
message.BodyEncoding = Encoding.GetEncoding("utf-8");
message.IsBodyHtml = true ;
message.Body = $"<p>(如無法使用手機簡訊,可列印本畫面代替)</p><img src='qr.png' />";
message.BodyEncoding = Encoding.GetEncoding("utf-8");
string attimg = Environment.CurrentDirectory + @"\qr.png";
string g = $"http://www.abcdefg.com.tw/event/attend.aspx?g={guid}&u={stuff.memberid}&sid={stuff.idschedule}";
var pic = generateQR(g); // 郵件內插QRCODE圖片
pic.Save(attimg);
Stream astream = Stream.Null;
System.Net.Mail.Attachment att1 = new Attachment(attimg);
att1.Name = System.IO.Path.GetFileName(attimg);
att1.NameEncoding = Encoding.GetEncoding("utf-8");
att1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
att1.ContentDisposition.Inline = true;
att1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
message.Attachments.Add(att1);
message.To.Add(new MailAddress( stuff.mailaddress,stuff.empCName,Encoding.GetEncoding("utf-8")));
smtpClient.Send(message);
}
謝謝j大
smtpClient.Send 寄件人不會亂碼,但SmtpClient.SendAsync
想再請問System.Net.Mail 好像沒辦法走 465 port (implicit port的樣子)只能587
我再參考一下您的程式碼 感謝~
試試
using (var client = new SmtpClient())
{
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Port = 465;
client.Host = "host";
client.EnableSsl = true;
client.Send(message);
}
不過官方文件說內建的不支援implicit SSL,上述方法可能也不行,那你可能就要動用到System.Web.Mail或NUGET下載 Rebex.Mail 或 SecureBlackbox
Nuget裡可以下載MailBee.NET可使用465 implicit SSL
https://afterlogic.com/mailbee-net/email-components