C#-發送mail
關於這個功能,我們來來回回了好幾次,
每次在公司測完後,到客戶那就會出包,
總是有幾封mail寄不出來,然後又改了好幾版,
不久前,公司同事發現如果用「gmail」的「smtp」來當主機,
居然成功發送而且沒有人遺漏,不多說,
立即就將之前的程式改掉~~
測試結果~~~成功 XD (可見客戶的主機是多麼的‥‥‥)
private void btnsendmail_Click(object sender, EventArgs e) { string fromEmail = "XXX@gmail.com"; string fromName = "C.H.H"; MailAddress from = new MailAddress(fromEmail, fromName, Encoding.UTF8); string toEmail = "YYY@gmail.com"; MailMessage mail = new MailMessage(from, new MailAddress(toEmail)); string subject = "Test Subject"; mail.Subject = subject; mail.SubjectEncoding = Encoding.UTF8; string body = "Test Body"; mail.Body = body; mail.BodyEncoding = Encoding.UTF8; mail.IsBodyHtml = false; mail.Priority = MailPriority.High; // SMTP Setting SmtpClient client = new SmtpClient(); client.Host = "smtp.gmail.com"; client.Port = 587; client.Credentials = new NetworkCredential("username@gmail.com", "password"); client.EnableSsl = true; // Send Mail client.SendAsync(mail, mail); // Sent Compeleted Eevet client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted); }
自我LV~