如何利用C#和Gmail帐号发送邮件
2011-10-19 22:48
399 查看
前段时间学习了一下,如何利用C#和Gmail帐号发送邮件。现在写下来来共享一下:
它的界面是这样的:
View Code
它的界面是这样的:
View Code
private void sendMailBtn_Click(object sender, EventArgs e) { pbSendEmail.Value = 0; pbSendEmail.Maximum = 100; try { MailMessage mail = new MailMessage(); SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); //mail from mail.From = new MailAddress("123@gmail.com"); //mail to if (string.IsNullOrEmpty(tbMailTo.Text)) { MessageBox.Show("The recipient of this mail can‘t be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); sendMailBtn.Enabled = false; return; } string mailAddress = tbMailTo.Text.TrimEnd(); string[] mailAddresses = mailAddress.Split(','); mailAddress = string.Empty; ArrayList emailList = new ArrayList(); foreach(string address in mailAddresses) { string tempEmailAddress = address.Trim(); if (string.IsNullOrEmpty(tempEmailAddress)) { continue; } if (!CheckEmail.IsEmail(tempEmailAddress)) { MessageBox.Show("Invalid E-mail address: " + address + "\nIf there are multiple e-mail addresses, please seperate each of them by comma character.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } emailList.Add(tempEmailAddress); } foreach (string tempEmailAddress in emailList) { mail.To.Add(tempEmailAddress); pbSendEmail.Value++; } //mail subject mail.Subject = tbSubject.Text.ToString(); //mail body if (cbBodyFormat.SelectedText == "Text") { mail.IsBodyHtml = false; } else { mail.IsBodyHtml = true; } mail.BodyEncoding = Encoding.UTF8; mail.Body = rtbMailBody.Text.ToString(); //mail attachment if (!string.IsNullOrEmpty(tbAttachment.Text.ToString())) { System.Net.Mail.Attachment attachment = new Attachment(tbAttachment.Text); mail.Attachments.Add(attachment); } //mail priority if (cbMailPriority.SelectedText.ToString() == "High") { mail.Priority = MailPriority.High; } else if (cbMailPriority.SelectedText.ToString() == "Low") { mail.Priority = MailPriority.Low; } else { mail.Priority = MailPriority.Normal; } smtpServer.Port = 587; smtpServer.Credentials = new System.Net.NetworkCredential("123", "password"); smtpServer.EnableSsl = true; smtpServer.Send(mail); pbSendEmail.Value = 100; MessageBox.Show("Your mail has been sent successfully, please check it!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("The following exception occurred: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } }
相关文章推荐
- C#中如何利用Gmail账户发送邮件
- 关于C#如何利用QQ邮箱SMTP发送邮件
- 如何在ASP.NET2.0中通过Gmail发送邮件
- 在AppServ Win32 中如何打开openssl,用PHPMailer发送gmail邮件
- Nodejs 如何发送邮件(Gmail 和 126邮箱)
- C# 利用SMTP异步发送邮件
- 如何在代理环境下发送邮件?——C#实现Socket代理方式的邮件发送
- C#发送邮件(Gmail到qq邮箱和163邮箱)
- 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)
- C#发送邮件(Gmail到qq邮箱和163邮箱)
- ios如何利用系统邮件发送邮件
- 利用GMail和126的SMTP发送邮件
- 使用C#通过GMAIL发送邮件.
- 如何在 Linux 上使用 Gmail SMTP 服务器发送邮件通知
- C#如何利用QQ邮箱SMTP发送邮件
- 利用asp.net自带邮件组件 发送邮件,支持126,163,gmail,qq等邮箱
- 举例说明如何在android中利用Service来实现后台发送邮件功能
- 利用asp.net自带邮件组件 发送邮件,支持126,163,gmail,qq等邮箱
- C#利用smtp服务器发送邮件
- C# 如何新建和发送带附件的邮件