无法发送带有多个收件人的附件的邮件 [英] not able to send mail with attachment to multiple reciepent

查看:104
本文介绍了无法发送带有多个收件人的附件的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有附件的邮件发送到多个接收方,但设法仅发送文本消息而不发送附件
从excel文件添加电子邮件地址
这是我的代码

I am trying to send mail with attachment to multiple reciepent but manage to send only text message not attachment
fatching email address from excel file
Here is my code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Mail;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    Excel ex = new Excel();
    String sql;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            bool uplod = true;
            string fleUpload = Path.GetExtension(FileUpload1.FileName.ToString());
            if (fleUpload.Trim().ToLower() == ".xls" |
        fleUpload.Trim().ToLower() == ".xlsx")
            {
                // Save excel file into Server sub dir
                // to catch excel file downloading permission
                FileUpload1.SaveAs(Server.MapPath("~/email/" +
                   FileUpload1.FileName.ToString()));
                string uploadedFile = (Server.MapPath("~/email/" +
                    FileUpload1.FileName.ToString()));
                try
                {
                    //dt = xlsInsert(uploadedFile);
                    //GridView1.DataSource = dt;
                    //GridView1.DataBind();
                }
                catch (Exception)
                {
                    uplod = false;
                    this.lblmessage.Text = "System uploading Error";
                }
                //File.Delete(uploadedFile); // Delete upload Excel
                //file in sub dir 'lsUploadFile' no need to keep...
            }
            if (uplod)
                    {
                string mess1 = "File has successfully uploaded.";
                this.lblmessage.Text = mess1;
            }
        }
        else
        {
            this.lblmessage.Text = "Please select file to upload !";
        }
    }
    protected void send_Click(object sender, EventArgs e)
    {
        ds.Clear();
        sql = "select email from [Sheet1$]";
        ds = ex.select(sql);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            int flag=0;
            if (ds.Tables[0].Rows[i]["email"].ToString() == "")
            {
                flag = 1;
                break;
            }

            #region Email
            MailMessage mail = new MailMessage();
            mail.To.Add(ds.Tables[0].Rows[i]["email"].ToString());
            mail.From = new MailAddress("deanpg@pacific-university.ac.in");
            mail.Subject =subject.Text.ToUpper();

            mail.Body = "<table width="100%><tr"><td width="40%>1."> Message  </td>" + "<td width="60%>"" mode="hold" />
            if (FileUpload2.HasFile)
            {
                mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
            }
            mail.IsBodyHtml = true;
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.Credentials =
                new System.Net.NetworkCredential("deanpg@pacific-university.ac.in", "pacific@phd");

            try
            {
                client.Send(mail);
                lblthen.Text = "Message has been sent succcessfully.";
            }
            catch (Exception ext)
            {
                lblmess.Text = ext.Message;
            }

            #endregion
        }
    }
}

推荐答案

"; ds = ex.选择(sql); for ( int i = 0 ; i < ds.Tables [ 0 ].Rows.Count; i ++) { int flag = 0 ; 如果(ds.Tables [ 0 ].行[i] [ 电子邮件"].ToString()== " ) { 标志= 1 ; break ; } #region电子邮件 MailMessage邮件= MailMessage(); mail.To.Add(ds.Tables [ 0 ].Rows [i] [" 电子邮件"].ToString()); mail.From = MailAddress(" ); mail.Subject = subject.Text.ToUpper(); mail.Body = " 100 %> < tr " %> " > 消息</td>" + " 60 %> " mode = " /> 如果(FileUpload2.HasFile) { mail.Attachments.Add(附件(FileUpload2.PostedFile.InputStream,FileUpload2.FileName)); } mail.IsBodyHtml = true ; SmtpClient client = SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true ; client.Host = " ; client.Port = 587 ; client.Credentials = System.Net.NetworkCredential(" pacific @ phd"); 尝试 { client.Send(邮件); lblthen.Text = " ; } 捕获(异常扩展) { lblmess.Text = ext.Message; } #endregion } } }
"; ds = ex.select(sql); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { int flag=0; if (ds.Tables[0].Rows[i]["email"].ToString() == "") { flag = 1; break; } #region Email MailMessage mail = new MailMessage(); mail.To.Add(ds.Tables[0].Rows[i]["email"].ToString()); mail.From = new MailAddress("deanpg@pacific-university.ac.in"); mail.Subject =subject.Text.ToUpper(); mail.Body = "<table width="100%><tr"><td width="40%>1."> Message </td>" + "<td width="60%>"" mode="hold" /> if (FileUpload2.HasFile) { mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName)); } mail.IsBodyHtml = true; SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; client.Host = "smtp.gmail.com"; client.Port = 587; client.Credentials = new System.Net.NetworkCredential("deanpg@pacific-university.ac.in", "pacific@phd"); try { client.Send(mail); lblthen.Text = "Message has been sent succcessfully."; } catch (Exception ext) { lblmess.Text = ext.Message; } #endregion } } }


http: //msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx [ http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=19 [^ ]
http: //weblogs.asp.net/nawaf/archive/2007/12/07/sending-email-in-c-sync-async-multiple-recipients-attachments-etc.aspx [ http://stackoverflow.com/questions/11776894/通过interop-library-c-sharp发送邮件到多个收件人 [带有多个附件的ASP.NET电子邮件 [
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^]
http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=19[^]
http://weblogs.asp.net/nawaf/archive/2007/12/07/sending-email-in-c-sync-async-multiple-recipients-attachments-etc.aspx[^]
http://stackoverflow.com/questions/11776894/sending-mail-to-multiple-recipients-via-interop-library-c-sharp[^]
ASP.NET email with multiple attachments[^]


这篇关于无法发送带有多个收件人的附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆