Sending mail from c# fails over proxy - c#

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

Related

C# smtp gmail not working on server, but working well in localhost

My application have function to sent email trough smtp.gmail.com
It works well when i run at local, but after i publish to vps, the email not sent.
dns setting for mx(10) was pointing to smtp.gmail.com
here's the code that i use :
public ServiceResult SendEmailOrder(string Email)
{
ServiceResult result = new ServiceResult();
try
{
const string fromPassword = "password";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("senderaddress#gmail.com");
mail.Subject = "subject mail";
mail.Body = "Body Mail";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("senderaddress#gmail.com", fromPassword);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail.To.Add(new MailAddress(Email));
SmtpServer.Send(mail);
}
catch (Exception ex)
{
result.Error("Failed Send Email", ex.InnerException.Message);
}
return result;
}
is that something that i missing?
*note :
- my vps was connect to internet,
- i use google cloud before, and the code works well.

Problems sending e-mail in web app

I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.
I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:
When I try using port 587 in the example below I get an error saying:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
When I try using port 465 in the example below:
My application just hangs forever and my page never gets back my email function which is in the PageLoad.
A couple of things to note just incase one of these is messing me up:
I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
I am using the same e-mail address for from, to, and gmail login credentials)
My ISP does not block these ports. (comcast cable)
I even went to the google DisplayUnlockCaptcha page to Allow access to my account
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
This code should work fine for you
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
You'll need to set the delivery mode otherwise gmail will return an error
EDIT:
Throwing an 'using' around 'MailMessage' might also be a smart thing to do
It turns out that it is because of a GMail security setting.
https://www.google.com/settings/security/lesssecureapps
You have to enable access for less secure apps.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/

Sending Email via Outlook but after amount of emails it throws exceptions

I've recently created an web application in C# where I send emails to one mailbox. Everything works fine for the first 10 to 15 emails. After that, it gives the error :
The Mailbox unavailable. The server response was: 5.7.3 Requested
action aborted; user not authenticated
I then have to authenticate my email address again in order for it to work.
I use outlook/hotmail to send the emails.
Has any one ever had a similar issue?
Code Example
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("email#outlook.com");
mail.To.Add("thisemail#address.com");
mail.Subject = "test1";
mail.IsBodyHtml = true;
mail.Body = "";
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("address#outlook.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Response.Write("success");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

Failure sending mail

I'm trying to make an email page section and I found an article and one of the answer in this forum which I used in my codes below but its still saying failure sending mail which I could hardly figure out why?. I need assistance in finding a remedy why my codes below fails to send an email. Here my codes below for your reference. Please advise...Thanks.
protected void Button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com"; //Im not sure about this,I just copy it from the article
client.Port = 4548; //This is my ASP.Net Development Server Port,Im not sure also if this is correct.
client.Credentials = new System.Net.NetworkCredential(
#"email_account",
#"email_password"); //Im not sure about this code if this correct, I just copy it
client.EnableSsl = true;
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text, "Mackmellow"); //Textbox4 is my email address
message.To.Add(new MailAddress(TextBox1.Text, "Mackmellow")); // Textbox1 is the email add I want to send
message.Subject = TextBox2.Text; //Textbox2 is my subject
message.Body = TextBox3.Text; // Textbox3 is my message
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// send message
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
Can please correct my codes and tell me what I'm missing? Thanks in advance...
For sending mail you can't use your development server port.
You have to use the smtp server port of your mail server.
for smtp.live.com you should use either port 25 or 587
For this code below, specify VALID login details (email/password)
client.Credentials = new System.Net.NetworkCredential(
#"email_account",
#"email_password");
Provide only G-mail Account Id.Then check it . I think SmtpClient Does not accept any other domain mail address . So you can put only gmail address then check it .
Edit
See this sample's and other answers .
Error sending mail from Asp.Net/C#
how to send email in asp.net to any destination eg yahoo,gmail,hotmail etc c# code
Try this.
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("email#hotmail.com");
mail.To.Add("ToGmail.com");
mail.Subject = "Your Sub";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "HTML code";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("email#hotmail.com", "YourPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

Can't send smtp email from network using C#, asp.net website

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.

Categories

Resources