Email Is not sending successfully from Asp.net application - asp.net

I am trying to send Email from My asp.net web application which i have developed only for practice purpose but it is not working at all i tried so many things, so many times from google and youtube also but it is not working at all. please do help me if possible.....
one more thing i am doing it in college campus and their firewall is on so i tried it from another network also still it is not working...???
public bool sendMail(string from, string to, string subject, string body)
{
bool flag = false;
var message = new MailMessage();
try
{
message.To.Clear();
message.To.Add(new MailAddress(to));
message.From = new MailAddress(from);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "Email#mail.com",
Password = "password"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(message);
}
flag = true;
}
catch (Exception ex)
{
}
return flag;
}

Related

ASP.NET MVC smtp error sending mail problem

I have already edited the webconfig file but I've got SmtpException "error sending mail". My domain port is 25. I've double checked. Here is my code. What's problem? Thank you.
public void SendEmail(string toAddress, string fromAddress,string subject, string message)
{
try
{
using (var mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(new MailAddress(toAddress));
mail.Subject = subject;
mail.Body = message;
mail.IsBodyHtml = true;
try
{
using (var smtpClient = new SmtpClient())
{
smtpClient.Host = "webmail.domain.com";
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential("blabla#domain.com", "pass");
smtpClient.Send(mail);
}
}
mail.Dispose();
}
}

How to use Parallel.ForEach with Grid View in ASP.NET

I had created an application to send bulk mail but my application is taking too much time to send all the emails. This was my code:
foreach (GridViewRow grow in GridView1.Rows)
{
string Emails = grow.Cells[0].Text.Trim();
string file = Server.MapPath("~/Mail.html");
string mailbody = System.IO.File.ReadAllText(file);
string to = Emails;
string from = "xyz#gmail.com";
MailMessage msg = new MailMessage(from, to);
msg.Subject = "Auto Response Email";
msg.Body = mailbody;
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.live.com", 25);
client.UseDefaultCredentials = false;
System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz#gmail.com", "password");
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = basicCredential;
try
{
client.Send(msg);
cnfrm.Text = "Email Sended Successfully";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
So for it's solution I tied to use the Parallel.ForEach but I am finding myself unable to use it with GridView.
Parallel.ForEach(GridView1.Rows, GridViewRow =>
{
string Emails = grow.Cells[0].Text.Trim();
string file = Server.MapPath("~/Mail.html");
string mailbody = System.IO.File.ReadAllText(file);
string to = Emails;
string from = "xyz#gmail.com";
MailMessage msg = new MailMessage(from, to);
msg.Subject = "Auto Response Email";
msg.Body = mailbody;
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.live.com", 25);
client.UseDefaultCredentials = false;
System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz689#gmail.com", "password");
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = basicCredential;
try
{
client.Send(msg);
cnfrm.Text = "Email Sended Successfully";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
});
Could anyone tell me how I can use it!!
I don't think you can access the Gridview rows like that with multiple threads, extract the text in your main thread then use foreach. Also take a look at Partitioner to use with ForEach
Here's an example of how it might look:
var emailtext = new List<string>()
foreach (GridViewRow grow in GridView1.Rows)
{
emailtext.Add(grow.Cells[0].Text.Trim());
}
//split and load balance the List
Partitioner<string> partitioner = Partitioner.Create(emailtext, true);
Parallel.ForEach(partitioner, mailtext =>
{
//your email sending code here
//NB: 'mailtext' is your string to send
}
Note: Most mail providers limit the number of emails you send per session or per hour, so you may need to consider this in your application.

Can't Send Emails From my Website

Here I am in a strange situation. When I send the email from localhost it is working fine but when I upload the page to the server and try to send email, I get the following error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I'm displaying the error message in a label for testing purpose and the try is also misplaced I know, I will set it later.
the code I am using is
if (Page.IsValid)
{
try
{
StringBuilder message = new StringBuilder();
message.Append("Hello My Name is ");
message.Append(txtName.Text);
message.AppendLine();
message.AppendLine("My Contact Number " + txtContactNumber.Text.ToString());
message.AppendLine();
message.AppendLine();
message.AppendLine("My Email Id Is " + txtFromEmailAddress.Text.ToString());
message.AppendLine();
message.Append(txtEmailBody.Text);
MailMessage mailMessage = new MailMessage("xxx#gmail.com", "yyy#gmail.com");
mailMessage.Subject = "New Client Query";
mailMessage.Body = message.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
//smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "xxx#gmail.com",
Password = "password"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
txtContactNumber.Text = "";
txtFromEmailAddress.Text = "";
txtName.Text = "";
txtEmailBody.Text = "";
lblEmailStatus.Text = "Email Sent Successfully.";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
catch(Exception ex)
{
lblEmailStatus.Text = ex.Message + " <br> " + ex.Source;
}
}
else
{
lblEmailStatus.Text = "Error! Email Not Sent ";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
I have googled for a couple of hours and checked links at this site as well but I still cant figure it out.
Now I request you guys here if any one have any solutions / hint.
Try this
public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress;
fromAddress = new MailAddress(FromMail);
smtpClient.Host = MailServer;
smtpClient.Port = 25;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = SMTPUserInfo;
message.From = fromAddress;
message.To.Add(new MailAddress(To, DisplayName));
if (CC != "")
message.CC.Add(new MailAddress(CC, DisplayName));
if (BCC != "")
message.Bcc.Add(new MailAddress(BCC, DisplayName));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = EmailMessage;
if (Attachment != "")
message.Attachments.Add(new Attachment(Attachment));
message.Priority = MailPriority.High;
smtpClient.Send(message);
return "SendEmail";
}
catch (Exception ex)
{
return "Email :" + ex;
}
}
i got the reason finally .
the email i was sending emails from was kind of hacked some days before and for the security reasons google team had kind of marked my email as unsecure . i changed the emails address and it is working fine thanks you all.
Based on the Google Gmail Documentation it would appear that the Port should be 587 not 25. I found a few other questions that seem to be related here and here.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;

How to Send Mail in Asp.net in C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No connection could be made because the target machine actively refused it?
I Face Critical Problem During Sending Mail in Asp.net please tell me Solution if Anyone know About this.
This Is My Code for Sending Mail
MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient();
mm.From = new MailAddress(txtfrom.Text);
mm.To.Add(new MailAddress(txtto.Text));
mm.Subject = txtsubject.Text;
mm.Body = txtmessage.Text;
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com"; //You can add this in the webconfig
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "mymail#gmail.com";
NetworkCred.Password = "my password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //this is Gmail port for e-mail
smtp.Send(mm);//send an e-mail
The Problem is That when i Click on Send Button it Show following Error
"No connection could be made because the target machine actively
refused it 173.194.79.109:587"
Please tell me Solution ..
you need to set false the UseDefaultCredentials property.
smtp.UseDefaultCredentials = false;
That error message usually means a firewall is blocking your connection. Instead of using your machine name "173.194.79.109:587", try using "localhost".
protected void SendMail_Click(object sender, EventArgs e)
{
var fromAddress = new MailAddress(fromid.Text, fromname.Text);
var toAddress = new MailAddress(toid.Text, toname.Text);
string fromPassword = pswd.Text;
string subject = subjectbox.Text;
string body = bodybox.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = false
})
{
smtp.Send(message);
}
}
This is the function which i checked to send mail...and it's working properly.
`
private static bool testsendemail(MailMessage messageBody)
{
try
{
MailMessage message1 = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
MailAddress fromAddress = new MailAddress("FromMail#Test.com");
message1.From = fromAddress;
message1.To.Add("ToMail#Test1.com");
message1.Subject = "This is Test mail";
message1.IsBodyHtml = true;
message1.Body ="You can write your body here"+messageBody;
smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("SenderMail#yahoo.com", "YourPassword");
smtpClient.Send(message1);
}
catch
{
return false;
}
return true;
}`
Thank You.
This is how I use it.
SmtpClient gglClient = new SmtpClient("173.194.67.108", 587);
gglClient.UseDefaultCredentials = false;
gglClient.DeliveryMethod = SmtpDeliveryMethod.Network;
gglClient.Credentials = new NetworkCredential("username#gmail.com", "password");
gglClient.EnableSsl = true;
MailMessage msg = new MailMessage("", email);
msg.Subject = "";
msg.Body = #" ";
msg.IsBodyHtml = false;
msg.BodyEncoding = UTF8Encoding.UTF8;
gglClient.Timeout = 8000;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; };
gglClient.Send(msg);

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