Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
following is the Code I am using to Send Email
through ASP.NET and C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Configuration;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void bt_sendMail_Click(object sender, EventArgs e)
    {
        SendMail(tb_GmailAccount.Text, tb_GmailPassword.Text, tb_RecieverEmail.Text, tb_Subject.Text, tb_Message.Text);
       
    }
    public  void SendMail(string gMailAccount, string password, string to, string subject, string message)
    {
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(gMailAccount);
            msg.To.Add(new MailAddress(to));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.Normal;
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(gMailAccount, password),
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network
            };

            client.Send(msg);

        }
        catch (Exception ex)
        {
            string s = ex.Message; ;
        }
    }
}


But getting Exception as SMTP EXCEPTION CAUGHT ..Failure to send.
Please Guide me where is the Problem
Posted
Comments
Dhinesh kumar.V 17-Nov-12 6:52am    
Use this one.. Hope this one is help you.


SmtpClient Client;
MailMessage Msg;
Attachment Anexo;

use this try block coding under button_click event
try
{
Client = new SmtpClient("smtp.gmail.com");
Msg = new MailMessage();
Anexo = new System.Net.Mail.Attachment(label7.Text);
Msg.From = new MailAddress(textBox1.Text);
Msg.To.Add(textBox4.Text);
Msg.Body = richTextBox1.Text;
Msg.Attachments.Add(Anexo);
Msg.Subject = textBox3.Text;
SmtpClient smtps = new SmtpClient("smtp.gmail.com", 587);
Client.Credentials = new NetworkCredential();
Client.Port = 587;
Client.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
Client.EnableSsl = true;
Client.Send(Msg);
}
catch
{
// MessageBox.Show("adf");
}
Karwa_Vivek 17-Nov-12 7:05am    
No its Not working

refer this

Failure sending mail[^]


Hope This Helps..
 
Share this answer
 
C#
using System.Net;
using System.Net.Mail;

public string ord, body;
MailMessage oMail = new MailMessage();
SmtpClient oSmtp = new SmtpClient("smtp.gmail.com", 587);

try
        {
            oMail.To.Add(b@b.com);
            oMail.From = new MailAddress("a@a.com");
            oMail.Subject = "Subject";

            body = "Hello, <br />";
            body += "How are you";

            oMail.Body = body;
            oMail.IsBodyHtml = true;
            oSmtp.UseDefaultCredentials = false;
            oSmtp.Credentials = new System.Net.NetworkCredential("a@a.com", "password");
            oSmtp.EnableSsl = true;
            oSmtp.Send(oMail);
        }
        catch { }
 
Share this answer
 

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