Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 17-Nov-12 0:50am
Comments
Dhinesh kumar.V at 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 at 17-Nov-12 7:05am
   
No its Not working
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

refer this
 
Failure sending mail[^]
 

Hope This Helps..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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, 
";
            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 { }
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100