csharp 发电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 发电子邮件相关的知识,希望对你有一定的参考价值。

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
using System.Net.Mail;

public static bool Send(MailMessage email)
        {
            var result = false;
            
            if (email != null && email.To.Count > 0)
            {
                using (SmtpClient smtp = new SmtpClient())
                {
                    try
                    {
                        email.From = new MailAddress("<from email address>");
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new System.Net.NetworkCredential("<from email id>", "<password>");
                        smtp.EnableSsl = true;
                        smtp.Send(email);
                        result = true;
                    }
                    catch(Exception ex)
                    {
                        //throw new Exception("Error in EmailMgr.Send(MailMessage). " +  ex.Message, ex);
                        result = false;
                    }
                }
            }
            return result;
        }
//using System.Net;
//using System.Net.Mail;

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("<smtp host address>");
 
                mail.From = new MailAddress("me@mydomain.com");
                mail.To.Add("u@urdomain.com");
                mail.Subject = "Subject";
                mail.Body = "body";

                SmtpServer.Port = 25; // Or whatever port your smpt server is supporting
                SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);

以上是关于csharp 发电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

csharp 发邮件

csharp 发邮件

csharp 发短讯()

csharp 发短讯()

csharp C#中的电子邮件发送

csharp 验证电子邮件()