使用asp.net向多个收件人发送邮件

时间:2015-09-21 12:15:41

标签: c# asp.net asp.net-mvc

我的asp.net mvc-5 Web应用程序中有以下代码: -

string from = "info@Oursystem.com"; 
                using (MailMessage mail = new MailMessage(from, "user1@Oursystem.com"))
                {

目前,当我尝试为电子邮件添加多个收件人时,如下所示: -

string from = "info@Oursystem.com"; 
                using (MailMessage mail = new MailMessage(from, "user1@Oursystem.com;user2@Oursystem.com"))
                {

它不会发送任何电子邮件,所以有人可以就此提出建议吗?

3 个答案:

答案 0 :(得分:2)

尝试使用以下命名空间。

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent
                .putExtra(
                        android.content.Intent.EXTRA_TEXT,
                        "Some text");
        PackageManager pm = v.getContext().getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(
                shareIntent, 0);
        for (final ResolveInfo app : activityList) {
            if ((app.activityInfo.name).contains("facebook")) {
                final ActivityInfo activity = app.activityInfo;
                final ComponentName name = new ComponentName(
                        activity.applicationInfo.packageName, activity.name);
                shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                shareIntent.setComponent(name);
                v.getContext().startActivity(shareIntent);
                break;
            }
        }

MailMessage将单个reciver转到

  

public MailMessage(MailAddress from,MailAddress to)

检查here

所以你必须为列表中的其他reciver添加对象

答案 1 :(得分:1)

试试这个 -

        string from = "info@Oursystem.com"; 
        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(from);
            mail.To.Add("user1@Oursystem.com");
            mail.To.Add("user2@Oursystem.com");
        }

new MailMessage()构造函数接受fromto的参数,并且应该在您只有一个收件人时使用。如果有多个收件人,则必须将其添加到mail.To集合

答案 2 :(得分:0)

public class MailHandler
{

public bool SendEMail(string smtpHost ="smtp.gmail.com", int port = 587, string senderMail , string  senderPass, ArrayList mailToArr, string subject, bool isHtml, string body)
    {
        try
        {

            SmtpClient smtpClient = new SmtpClient(smtpHost, port);
            smtpClient.UseDefaultCredentials = false;// true;
            smtpClient.Credentials = new System.Net.NetworkCredential(senderMail, senderPass);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(senderMail);
            for (int i = 0; i < mailToArr.Count; i++)
            {
                mail.To.Add(new MailAddress((string)mailToArr[i]));
            }
            mail.Subject = subject;
            //mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
            mail.Body = body;
            mail.IsBodyHtml = isHtml;
            mail.Priority = MailPriority.Normal;

            smtpClient.Send(mail);

            return true;
        }
        catch (Exception ex)
        {
            return false;

            // write exception on server log

        }

    }
}

//调用函数

                ArrayList mailToArr = new ArrayList();
                mailToArr.Add("to first email address");
                mailToArr.Add("to second email address");
                mailToArr.Add("to third email address");

                MailHandler objMailhandler = new MailHandler();
                string subject = "subject";
                string body = "<h1>  Password Request </h1> " ;


                objMailhandler.SendEMail("smtp.gmail.com", 587, senderMail , senderPass,mailToArr, subject, true, body);