Click here to Skip to main content
15,886,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need source code to send mails to many where am retrieving the sender address seperated by a comma from a text box

[edit] It shows smtp authentication needed [/edit]


C#
public partial class Mail : Form
    {
        public string s;
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        String[] email = new String[30];
        int flag = 0;
        public System.Net.NetworkCredential smtpcrede = new System.Net.NetworkCredential("username", "Password");


//Sending mail

 private void SendEmail(string sendTo, string sendFrom, string subject, string body)
        {
            try
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smtpcrede;
                client.EnableSsl = true;
                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);
                msg.IsBodyHtml = true;

                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                string[] addrs = sendTo.Split(',');
                foreach (string addr in addrs)
                {
                    MailAddress to = new MailAddress(addr);
                    msg.To.Add(to);
                }
              
                client.Send(msg);

            }


//send email 

    private void btn_Send_Click(object sender, EventArgs e)
        {
            
           String mailmsg = "text1<br/>"+rich_Msg.Text+"<br/>text2";
           

              for (int i = 0; i < email.Length; i++)
             {
                 SendEmail(email[i], "from@mail.com", txt_Subj.Text,mailmsg);
                // Console.WriteLine(email[i], "from@mail.com", txt_Subj.Text, rich_Msg.Text);

             }
Posted
Updated 7-Jul-13 22:43pm
v3
Comments
Sushil Mate 8-Jul-13 1:52am    
What you have done apart from posting question here?
MohRizwan 8-Jul-13 1:55am    
i have code but its not functioning properly ...
Sushil Mate 8-Jul-13 1:57am    
Great, Can you put here? & point out exactly where you are getting error/exception? your question will get more appreciation/respect if you show some code/efforts instead of asking the code.
MohRizwan 8-Jul-13 2:57am    
/* here is my code mr. sushil and it shows smtp authentication needed */

public partial class Mail : Form
{
public string s;
public SmtpClient client = new SmtpClient();
public MailMessage msg = new MailMessage();
String[] email = new String[30];
int flag = 0;
public System.Net.NetworkCredential smtpcrede = new System.Net.NetworkCredential("username", "Password");


//Sending mail

private void SendEmail(string sendTo, string sendFrom, string subject, string body)
{
try
{
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpcrede;
client.EnableSsl = true;
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
msg.IsBodyHtml = true;

msg.Subject = subject;
msg.Body = body;
msg.From = from;
string[] addrs = sendTo.Split(',');
foreach (string addr in addrs)
{
MailAddress to = new MailAddress(addr);
msg.To.Add(to);
}

client.Send(msg);

}


//send email

private void btn_Send_Click(object sender, EventArgs e)
{

String mailmsg = "text1<br/>"+rich_Msg.Text+"<br/>text2";


for (int i = 0; i < email.Length; i++)
{
SendEmail(email[i], "from@mail.com", txt_Subj.Text,mailmsg);
// Console.WriteLine(email[i], "from@mail.com", txt_Subj.Text, rich_Msg.Text);

}

1 solution

C#
using System;
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.Mail;

public partial class _Default : System.Web.UI.Page 
{
    string type;
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Enabled = false;
        TextBox2.Enabled = false;
        TextBox3.Enabled = false;
        TextBox4.Enabled = false;
        TextBox5.Enabled = false;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (DropDownList1.SelectedValue == "gmail")
            {
                type = "smtp.gmail.com";
            }
            else
            {
                type = "smtp.live.com";

            }
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(TextBox1.Text);
            msg.To.Add(TextBox2.Text);
            msg.Subject = TextBox4.Text;
            msg.Body = TextBox3.Text;
            SmtpClient sc = new SmtpClient(type);
            sc.Port = 25;
            sc.Credentials = new NetworkCredential(TextBox1.Text, TextBox5.Text);
            sc.EnableSsl = true;
            sc.Send(msg);
            Response.Write("Mail sent");

        }
        catch (Exception e1)
        {
            Response.Write(e1.Message);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

       
        TextBox1.Enabled = true;
        TextBox2.Enabled = true;
        TextBox3.Enabled = true;
        TextBox4.Enabled = true;
        TextBox5.Enabled = true;
    }
}

If u want to send to multiple people..just add a loop in the MailMessage section upto Response.Write("Mail sent");

Accept as answer if it solves your problem
 
Share this answer
 
v2
Comments
MohRizwan 8-Jul-13 3:05am    
i am trying to run in desktop application any way thanks for your reply
Rajan Maheshwari 8-Jul-13 3:14am    
this is a desktop c# application
Rajan Maheshwari 8-Jul-13 3:15am    
Its absolutely working fine with me
MohRizwan 8-Jul-13 3:45am    
i am using a form application it shows error "namespace missing"
Rajan Maheshwari 8-Jul-13 4:09am    
Obviously that you have to resolve.I have provided you with all the functions that can help to send a mail through your gmail id.I did in asp.net thats why..you just add a namespace above

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