C#编程-140:Net.Mail类发送邮件
【摘要】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using ...
- 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;
- namespace MailTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- MailMessage mail = new MailMessage();
- string myEmail = txtAccount.Text;
- string myPwd = txtPassword.Text;
- mail.BodyEncoding = System.Text.Encoding.UTF8;
- mail.IsBodyHtml = true;
- mail.From = new MailAddress(myEmail);
- mail.To.Add(new MailAddress(txtAccept.Text));
- mail.Subject = txtSubject.Text;
- mail.Body = txtBody.Text;
- mail.BodyEncoding = Encoding.UTF8;
- SmtpClient client = new SmtpClient(txtServer.Text);
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential(myEmail.Substring(0,myEmail.IndexOf('@')),myPwd);
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- try
- {
- client.Send(mail);
- MessageBox.Show("发送成功!");
- txtSubject.Text = null;
- txtBody.Text = null;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/81049062
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)