Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
        }
    }
}
Posted
Updated 27-Sep-12 21:24pm
v2
Comments
ajitkadam 28-Sep-12 3:25am    
Hi Please tell me more details on this. I will try solve this issue.

Thanks in Advance,

Regards,
Ajit Kadam
ajitkadam2003@gmail.com
1811421 28-Sep-12 4:12am    
i am able to send mail only on gmail but not on yahoo and other.the text message is send on yahoo and other

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900