Tuesday, 28 February 2012

mail send with asp.net with google app email id create


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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblmsg.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {

            //MailAddress fromAddress = new MailAddress(txtEmailId.Text);
         
          //  message.From = fromAddress;


            message.From = new MailAddress(txtEmailId.Text.ToString(), txtEmailId.Text, System.Text.Encoding.UTF8);

            message.To.Add("******@yahoo.co.in");
            message.CC.Add("**********@********.com");
          message.Bcc.Add("**********@*********.com");
            message.IsBodyHtml = true;
           
            message.Subject = "testing mail";
            message.Body = "Name=" + txtName.Text + "<br/>Email Id = " + txtEmailId.Text + "<br/>" + txtContactNo.Text + "<br/><hr/>" + "Contact Query :" + txtCQuery.Text;
            smtpClient.EnableSsl = true;


            //SmtpClient mySmtpClient = new SmtpClient();
            System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("*******@gmail.com", "**********");
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = myCredential;
            smtpClient.ServicePoint.MaxIdleTime = 1;

            smtpClient.Send(message);
            message.Dispose();

           // smtpClient.Send(message);
         
            lblmsg.Visible = true;
            lblmsg.Text = "We Will Contact You Soon......";

            Clertxt();
        }
        catch (Exception ex)
        {
            lblmsg.Text = "Send Email Failed." + ex.Message;
        }




    }
    private void Clertxt()
    {
        txtName.Text = ""; txtEmailId.Text = ""; txtContactNo.Text = ""; txtCQuery.Text = "";
    }
   
}

google app email send mail in asp.net


Web.config Setup:
  <system.net>

    <mailSettings>

      <smtp from="noreply@yourdomain.com">

        <network host="smtp.gmail.com" password="******" userName="noreply@yourdomain.com" port="587" />

      </smtp>

    </mailSettings>

  </system.net>


C# Send Mail Code:
            try
 
            {
 
                MailMessage message = new MailMessage();
 
 
                message.From = new MailAddress(tbxEmail.Text);
 
                message.To.Add(new MailAddress(_supportEmail));
 
 
                message.Subject = tbxSubject.Text;
 
                message.IsBodyHtml = false;
 
                message.Priority = MailPriority.High;
 
                message.Body = tbxMessage.Text;
 
 
 
                SmtpClient client = new SmtpClient();
 
                client.EnableSsl = true;
 
                client.Send(message);
 
 
                return true;
 
            }
 
            catch (Exception ex)
 
            {
 
                ExceptionHandler handler = new ExceptionHandler();
 
                handler.LogException(ex,
 
                    SeverityLevel.Critical,
 
                    "Contact Form Message could not be sent to support email",
 
                    GetType(),
 
                    "SendMessageEmail");
 
            }

//---------------email code
using System.Net.Mail;
using System.Net;
... 
SmtpClient mailClient = new SmtpClient(
    "smtp.gmail.com", "587");
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential(
    "your_gmail_username",
    "your_gmail_password");
mailClient.Credentials = cred;
mailClient.Send("from_me@gmail.com", "to_you@gmail.com",
    "subject goes here", "email body goes here");
//----------------------------- smtp code

//yahoo ---> smtp.mail.yahoo.com (port 25) 
//gmail ---> smtp.gmail.com (SSL enabled, port 465) 
//AOL ---> smtp.aol.com 
//Netscape ---> smtp.isp.netscape.com (port 25)

//--------copde

MailMessage mailMessage = new MailMessage();

mailMessage.To.Add("someone@somewhere.com");

mailMessage.Subject = "Test";

mailMessage.Body = "<html><body>This is a test</body></html>";

mailMessage.IsBodyHtml = true;



// Create the credentials to login to the gmail account associated with my custom domain

string sendEmailsFrom = "emailAddress@mydomain.com";             

string sendEmailsFromPassword = "password";

NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);



SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

mailClient.EnableSsl = true;

mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

mailClient.UseDefaultCredentials = false;

mailClient.Timeout = 20000;

mailClient.Credentials = cred;

mailClient.Send(mailMessage);
<configuration>

  <system.net>

    <mailSettings>

      <smtp from="example@domain.com" deliveryMethod="Network">

          <network host="smtp.gmail.com" port="587"

              userName="example@domain.com" password="password"/>

      </smtp>

    </mailSettings>

  </system.net>

</configuration>
End when you send email just enable SSL on your SmtpClient:
var message = new MailMessage("navin@php.net");

// here is an important part:

message.From = new MailAddress("example@domain.com", "Mailer");

// it's superfluous part here since from address is defined in .config file

// in my example. But since you don't use .config file, you will need it.



var client = new SmtpClient();

client.EnableSsl = true;

client.Send(message);



//------------
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
This is my _AppStart.cshtml content :
@{
    WebSecurity.InitializeDatabaseConnection("usersdb", "UserProfile", "UserId", "Email", true);
    WebMail.SmtpServer = "smtp.gmail.com";
    WebMail.EnableSsl = true;
    WebMail.SmtpPort = 587;
    WebMail.UserName = "no-reply@imperialcinemas.net";
    WebMail.Password = "**********";
    WebMail.From = "no-reply@imperialcinemas.net";
}



using System.Net.Mail;
private void SendMail()
{
MailMessage message = new MailMessage();



message.To.Add( "testing@anydomain.com" ); 



message.From = new MailAddress( "email@mydomain.com", "Name to Display"
System.Text.Encoding.UTF8 );



message.Subject = "Testing";



message.SubjectEncoding = System.Text.Encoding.UTF8; 



message.Body = "Testing 123";



message.BodyEncoding = System.Text.Encoding.UTF8;



message.IsBodyHtml = false;



message.Priority = MailPriority.Normal;



SmtpClient emailClient = new SmtpClient();



emailClient.Credentials =  new NetworkCredential( "username@mydomain.com", "yourpassword" );



emailClient.Port = 587;



emailClient.Host = "smtp.gmail.com"



emailClient.EnableSsl = true; 



emailClient.Send( message );



}