I want to code to send mail and message in asp.net - asp.net

This is my code:
string from = "rashmi#example.com";
string to = "rashmi#example.com";
string mailSubject = "mail demo";
string mailBody = "mail sent successfully";
MailMessage mess = new MailMessage(from, to, mailSubject, mailBody);
mess.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("192.168.43.1", 25); //Server ip & port
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = new System.Net.NetworkCredential("rashmi#example.com", "****password****");
try
{
emailClient.Send(mess);
Response.Write("message sent");
}
catch (Exception ex)
{
Response.Write("Exception caught in CreateTestMessage1(): {0}"+ ex.ToString());
}
but I get this error:
Exception caught in CreateTestMessage1(): {0}System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 192.168.43.1 (192.168.43.1:25), connect error 10061
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) at mail_demo_project.register_mail_demo.submit_Click(Object sender, EventArgs e) in e:\dot net\projects\mail demo project\mail demo project\register_mail_demo.aspx.cs:line 53
error occured...

Use this code to send email:
string From = "fromemailaddress#gmail.com";
MailAddress mailAddress = new MailAddress(From, "Contact Us");
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("toemailaddress#gmail.com");
mailMessage.From = mailAddress;
mailMessage.Subject = Txt_Subject.Text;
string mailBody = "User Email: " + Txt_msg.Text + "<br/> User Name: " + Txt_Name.Text + "<br/> Message: " + Txt_msg.Text ;
mailMessage.Body = mailBody;
mailMessage.Priority = MailPriority.Normal;
mailMessage.IsBodyHtml = true;
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(From, "password");
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = credential;
smtpClient.Send(mailMessage);
and you have to make some security modification in your gmail account in security section :Allow less secure apps: YES

Related

I want to send email through my ASP.Net website

the email code works on local host but when i upload the website on server it shows error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
//Formatted
protected void btnSend_Click(object sender, EventArgs e)
{
var fromAddress = "djdanny1255#gmail.com";
string email = "djdanny1255#gmail.com";
var toAddress = email;
const string fromPassword = "********";
string subject = "Email=" + txtEmail.Text + " Phone=" + txtMobile.Text;
string body = txtMessage.InnerText;
try
{
using (MailMessage mm = new MailMessage(fromAddress, email))
{
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
catch (Exception ex)
{
Response.Write("Error" + ex.Message);
}
in first cas, i suggest you to change code at this code :
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress,fromPassword);
smtp.Send(mail);
}
}
and second after your Login in to your email, CLICK HERE .
This will see this page
i hope this help you ^^

SMTP Mail code working form local host but not fom server

SMTP mail code working from ASP .net local server but not from server here is my code :
protected void btn_send_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("to gmail address");
mail.From = new MailAddress("from gmail address", "Email head", System.Text.Encoding.UTF8);
mail.Subject = "This mail is send from asp.net application";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("from gmail address", "your gmail account password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mail);
Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
}
This is not working as per expected. I'm trying to send an email from feedback form.

Sending mail in asp.net using Exchange Server(smtp-outlook.office365.com)

We are working in Mailing system in asp.net,our client using Microsoft exchange server outlook.office365. We are getting this exception.
"System.Net.Mail.SmtpException: The operation has timed out " error .Please help us.
btn_click(){
try{SmtpClient smtp = new SmtpClient("smtp.outlook.office365.com");
smtp.Host = "outlook.office365.com";
smtp.Port = System.Convert.ToInt32("443");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("userid", "mypwd");
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.TargetName = "STARTTLS/outlook.office365.com";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("frommailid", "username");
msg.To.Add(new MailAddress("xxx#xxx.com"));
msg.Subject = "Test";
msg.Body = "Test mail ";
smtp.Timeout = 60000;
smtp.Send(msg);
}catch(exception ex)
{
throw ex;
}
}
Refer the below code snippet and try this out. It worked for me.
Remember: From field should be same email id which is used as an SMTP user. So in this example, xxx#blabla.onmicrosoft.com should be the From email id.
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(ToField.Text));
mail.From = new MailAddress(FromField.Text); //From field should be same email id which is used as a SMTP user.
mail.Subject = "Email Subject";
mail.Body = "Message Body";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxx#blabla.onmicrosoft.com", "Password");
client.Port = 587;//You can use Port 25 if 587 is blocked
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.TargetName = "STARTTLS/smtp.office365.com";
try {
client.Send(mail);
}
catch (Exception ex) {
ResponseLabel.Text = "Error:" + ex.Message;
}

Sending Email through Smtp

i'm unable to send mail, exception "Failure sending mail"
what should i do to correct the code?
MailMessage mailMessage = new MailMessage("yourmail#mail.com", ToEmail);
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("<b>Registration Approval for</b></br>");
sbEmailBody.Append(InstituteName + "<br/><br/>");
sbEmailBody.Append("Thanks for your registration ");
sbEmailBody.Append("<br/>");
mailMessage.IsBodyHtml = true;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.Subject = "Confirmation Mail!";
SmtpClient smtpClient = new SmtpClient("smtp.mail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "yourmailn#mail.com",
Password = " "
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);

How can i send a comment in email?

I have some text boxes like name,email address,phone no. and comment on my page.
I have to send the values to my email address..
How should I do this??
I am doing:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("xyz#gmail.com");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = txtComment.Text;
SmtpClient.Send(message);
Response.Write("Email successfully sent.");
}
catch (Exception ex)
{
Response.Write("Send Email Failed." + ex.Message);
}
}
and I am getting the following error:
An object reference is required for the nonstatic field, method, or property 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)'
SmtpClient.Host = "localhost";
SmtpClient.Port = 25;
~~~~~~~~~~~~~~~~~~~~
SmtpClient.Send(message);
These lines are attempting to use members of the class SmtpClient. However, as these members are not defined as static, you need to refer to your instance of that class, which you have called client.
Try
client.Host = "localhost";
client.Port = 25;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
Also, have a read of this article on the differences between class and instance members.
Finally, as SmtpClient implements IDisposable, I would change your code to wrap it in a using block, as this will ensure you are correctly cleaning up your SMTP session after you are finished with it.
using (SmtpClient client = new SmtpClient())
{
// YOUR CODE
}
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
if (to == "")
to = fromAddress.Address;
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = fromAddress;
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = fromAddress;
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, "XXXX");//"XXXX" is the password of the sender.
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}
i had done this:-
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage mailmsg = new MailMessage();
string name = txtName.Text.ToString();
string contact = txtPhoneNo.Text.ToString();
string comment = txtComment.Text.ToString();
string checkIn = txtCheckIn.Text.ToString();
string from = txtEmail.Text.ToString();
mailmsg.To.Add("recipientemailid#gmail.com");
mailmsg.From = new MailAddress(from.Trim());
mailmsg.Subject = "Test message";
mailmsg.Body = "This Query is submitted by user " + name.Trim() + ", "+ contact.Trim() + ", " + comment.Trim() + ", " + checkIn.Trim();
mailmsg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential("recipientemailid#Gmail.com", "password");
client.Credentials = credentials;
try
{
//try to send the mail message
client.Send(mailmsg);
Response.Write("sent!");
}
catch
{
//some feedback if it does not work
Response.Write("failed!");
}
}
Try
client.Host = "localhost";
client.Port = 465;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);
Do not try to use or manage disposable emails with this because you will be clearly exposed to spam-trappers. Your code is open and very easy to identify who is using the code and from where.

Resources