I am attending to send email via the following function and while it works fine when I run it from the local server, it fails when I run it remotely. What might be causing this problem?
private void SendEmail()
{
try
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
NetworkCredential loginInfo = new NetworkCredential("xx#gmail.com", "xxxx");
message.From = new MailAddress("xx#gmail.com", "xx");
message.To.Add(new MailAddress("yyy#zzz.ac.in","Mail"));
message.IsBodyHtml = true ;
string emailContent = "ICHE 2010 - Abstract Received <br><br>Title: " + Abstract_Title.Text + "<br><br>Author: " + TxtAuthor_FirstName.Text + "_" + TxtAuthor_LastName.Text + "<br><br>Abstract in pdf format attached with this email. <br><br> ICHE2010 Website";
message.Body = emailContent;
message.Subject = "ICHE 2010 - Abstract Received";
string FileName = Server.MapPath(Request.ApplicationPath + "\\AbstractPdfs" + "\\" + abstractBO.AbstractFileNameWithTicks);
Attachment attachmentpdf = new System.Net.Mail.Attachment(FileName);
message.Attachments.Add(attachmentpdf);
client.EnableSsl = true;
client.Send(message);
}
catch (SmtpException smtpex)
{
throw smtpex;
}
catch (Exception ex)
{
throw ex;
}
}
May be the firewall is blocking your application from sending email using the port. Or your remote server may not have internet connection. There can be many reasons for this. Please explain more.
You need to check two things:
From your code, check to see if port 587 is not blocked, or is enabled
Also try opening port 25, which is the port traditionally used by SMTP
Related
The following is my code to send a mail over a http proxy. I have set my proxy to the same as mentioned. Now when I try to run the program, after some time it says *System.Net.WebException: The remote name could not be resolved. May I know where I am going wrong?
protected void mailto(string message)
{
WebRequest.DefaultWebProxy = new WebProxy("10.1.1.4", 8080);
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail#gmail.com");
mail.To.Add("to_address");
mail.Subject = "For Data Using";
mail.Body = message;
SmtpServer.Port = 465;// Tried even with 587, but no luck
SmtpServer.Credentials = new System.Net.NetworkCredential("mymail#gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Debug.WriteLine(Environment.NewLine + "message sent");
}
catch (Exception ex)
{
Debug.WriteLine("Message not sent" + ex.ToString());
}
}
I have faced same issue with sending a mail from c# using Gmail SMTP, the problem was the Port.
Try to use the port : 587
SmtpServer.Port = 587;
Also don't forget to Enable POP & IMAP Access to your Gmail account, go to settings , choose 'Forwarding and POP/IMAP' from the tabs and enable the POP & IMAP
I have a general mailbox that could be used to send emails from my website. It has stopped working and throws an exception. The following is a screenshot of the error.
error http://img7.imageshack.us/img7/7227/42097647.png
The code is
protected void Submit_Click(object sender, EventArgs e)
{
if (ValidateBox.Text == "6")
{
MailMessage message = new MailMessage();
message.From = new MailAddress(EmailBox.Text.ToString());
message.To.Add(new MailAddress("praveendaniel86#gmail.com"));
message.Subject = "Message via CAM Website General Mailbox";
string body = "Name: " + NameBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
"Home Tel: " + HomeTelBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
"Work Tel: " + WorkTelBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
"Comment/Question: " + CommentBox.Text.ToString();
message.Body = body;
SmtpClient client = new SmtpClient();
client.Send(message);
Response.Redirect("~/Thank-You.aspx");
}
else
{
Label1.Visible = true;
}
}
Could this be an error from the SMTP Client mentioned in my webconfig, How can I check if its working fine ? Thanks.
You are not setting up the Network Credentials. In other words, you're not authenticating the email.
NetworkCredential myCredentials = new NetworkCredential(
"myemail#domain.com","myPassword","mail.myDomainName.com");
client.Credentials = myCredentials;
This is a configuration issue.
The SMTP server specified in your web.config (system.net > mailSettings > smtp) is not configured for relaying. You have two options to fix this:
Configure the SMTP server to allow relaying for the IP address of your web server (only!).
Send SMTP credentials with SmtpClient (see Dave's answer for that).
I'd be grateful if someone could tell me if I'm on the right track... Basically, I have a webservice i need to run for my app, I've put it into a try catch, if the try fails I want the catch to send me an email message with the details of the exception.
try
{
// run webservice here
}
catch (Exception ex)
{
string strTo = "scott#...";
string strFrom = "web#...";
string strSubject = "Webservice Not Run";
SmtpMail.SmtpServer = "thepostoffice";
SmtpMail.Send(strFrom, strTo, strSubject, ex.ToString());
}
Yes you are but you'd better wrapp yor exception handler in some kind of logger or use existing ones like Log4Net or NLog.
A quick and easy way to send emails whenever an Exception occurs can be done like this:
SmtpClient Server = new SmtpClient();
Server.Host = ""; //example: smtp.gmail.com
Server.Port = ; //example: 587 if you're using gmail
Server.EnableSsl = true; //If the smtp server requires it
//Server Credentials
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "youremail#gmail.com";
credentials.Password = "your password here";
//assign the credential details to server
Server.Credentials = credentials;
//create sender's address
MailAddress sender = new MailAddress("Sender email address", "sender name");
//create receiver's address
MailAddress receiver = new MailAddress("receiver email address", "receiver name");
MailMessage message = new MailMessage(sender, receiver);
message.Subject = "Your Subject";
message.Body = ex.ToString();
//send the email
Server.Send(message);
Yes this the right way, if you don't want to use any logger tool.You can create function SendMail(string Exceptioin) to one of the your common class and than call this function from each catch block
I want to send email with my web application. It is published on rackspace dedicated server but I'm using GoDaddy's SMTP server to send email.
The fault I'm getting is:
System.Net.Mail.SmtpFailedRecipientException: Mailbox name not allowed. The server response was: sorry, relaying denied from your location [xx.xx.xxx.xx] (#5.7.1)
This is my code
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net", 25);
string to ="rpanchal#itaxsmart.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("EmailId#domain.com","**");
MailAddress fromAddress = new MailAddress("myEmailId#domain.com", "CompanyName");
MailMessage message = new MailMessage();
message.From = fromAddress;
message.To.Add(to);
message.Body = "This is Test message";
message.Subject = "hi";
client.Send(message); message.Dispose(); return "Email Send";
Should I do any configuration on dedicated server?
Are you testing locally? If yes, then your SMTP server may not allow relaying. Do not worry when you will deploy the application there won't be any problem.
If you are hosting with RackSpace you should use the SMTP RackSpace recommends for sending from their servers. Unfortunately, you can only use relay-hosting.secureserver.net if you are sending from go Daddy Shared or 4GH hosting.
Start trying to change your port 465 instead 25.
Or remember Relay-hosting is very limited to 250 emails per day and not accept remote connections so easy. Check if you can use SSL connection.
That is so simple:
You must focus on smtp host, port, ssl...
Change smtp host to: relay-hosting.secureserver.net
And DELETE port and ssl, thats all...
Do not use smtp port and smtp ssl true or false
var fromAddress = "mailfrom#yourdomain";
// any address where the email will be sending
var toAddress = "mailto#yourdomain";
//Password of your mail address
const string fromPassword = "******";
// Passing the values and make a email formate to display
string subject = TextBox1.Text.ToString();
string body = "From: " + TextBox2.Text + "\n";
body += "Email: " + TextBox3.Text + "\n";
body += "Subject: " + TextBox4.Text + "\n";
body += "Message: \n" + TextBox5.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "relay-hosting.secureserver.net";
**//Warning Delete =>//smtp.Port = 80;**
**//Warning Delete =>//smtp.EnableSsl = false;**
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
I have my code here, it works fine from my home, where my user is administrator, and I am connected to internet via a cable network.
But, problem is when I try this code from my work place, it does not work. Shows error:
"unable to connect to the remote server"
From a different machine in the same network:
"A socket operation was attempted to an unreachable network 209.xxx.xx.52:25"
I checked with our network admin, and he assured me that all the mail ports are open [25,110, and other ports for gmail].
Then, I logged in with administrative privilege, there was a little improvement, it did not show any error, but the actual email was never received.
Please note that, the code was tested from development environment, visual studio 2005 and 2008.
Any suggestion will be much appreciated.
Thanks in advance
try
{
MailMessage mail_message = new MailMessage("xxxxx#y7mail.com", txtToEmail.Text, txtSubject.Text, txtBody.Text);
SmtpClient mail_client = new SmtpClient("SMTP.y7mail.com");
NetworkCredential Authentic = new NetworkCredential("xxxxx#y7mail.com", "xxxxx");
mail_client.UseDefaultCredentials = true;
mail_client.Credentials = Authentic;
mail_message.IsBodyHtml = true;
mail_message.Priority = MailPriority.High;
try
{
mail_client.Send(mail_message);
lblStatus.Text = "Mail Sent Successfully";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
}
catch (Exception ex)
{
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
Here is some sample code that works for me and talks to a gmail server
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
Try setting UseDefaultCredentials to false.
Also try switching SSL on for y7mail
mail_client.EnableSSL = true;
Source - http://help.yahoo.com/l/au/yahoo7/edit/registration/registration-488246.html
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
This is the best answer for System.Exception Problem. Totally verified, you need to provide username and password at network credentials. Also try to give this solution to other sites.