关闭

c#发送邮件

226人阅读 评论(0) 收藏 举报
private void sendMail()
        {
            string s_from = "aaa@htdchina.com";
            string s_to = "bbb@htdchina.com";
            string pwd = "pwd";
            string s_body = "测试邮件";
            s_body = s_body.Replace("/r/n", "<br/>");
            s_body = s_body.Replace("/n", "<br/>");
            s_body = s_body.Replace(" ", "&nbsp");
            int i = s_from.IndexOf("@");
            string username = s_from.Substring(0, i);
            MailAddress from = new MailAddress(s_from);
            MailAddress to = new MailAddress(s_to);
            MailMessage mailobj = new MailMessage(from, to);
            mailobj.Subject = "my title";
            mailobj.Body = s_body;
            mailobj.IsBodyHtml = true;
            mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            mailobj.Priority = MailPriority.High;
          
            mailobj.Attachments.Add(new Attachment("c://test.jpg"));
          
            SmtpClient smtp = new SmtpClient("mail.htdchina.com");
            // smtp.Host = "smtp.163.com";
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(username, pwd);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            try
            {
                smtp.Send(mailobj);
                MessageBox.Show("success");

            }
            catch
            {
                MessageBox.Show("error");
            }
        }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18019次
    • 积分:417
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:9篇
    • 译文:0篇
    • 评论:0条
    文章分类