I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147
Related
I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.
In C# I try to send email using Gmail. This is my code:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail#gmail.com");
mail.To.Add("myemailto#gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 465;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypsw");
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
And I get error:
{System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: The connection was closed.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
There is answer somewhere but i do not recall where so i will write you code below. If someone find answer please put in comment and i will point it there.
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("yourmail#gmail.com", "yourpw")
};
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = "Subject";
msg.From = new MailAddress("yourmail#gmail.com");
msg.Body = "Body here";
msg.Bcc.Add(li[i].Value);
smtpClient.Send(msg);
From the docs on EnableSsl Property:
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.
And long story short from other web sources is that port 465 expects SSL to be negotiated at connection setup (not supported by SmtpClient) whereas 587 uses STARTTLS after initial non secure dialog (supported by SmtpClient)
I need to send email from my site by SMTP protocol.
My program code works fine if I set email sender as gmail email, but when I put my hosting configuration as a sender it doesn't work.
I get this response:
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 162.249.5.6:25"
My code is below.
SmtpClient smtpClient = new SmtpClient("segrad-center.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "test#segrad-center.com",
Password = "123456789segrad"
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
MailMessage mailMessage = new MailMessage("test#segrad-center.com", "khalilyehia12#gmail.com");
mailMessage.Subject = "test";
mailMessage.Body = "a2hostiong";
smtpClient.Send(mailMessage);
You can test this email by this link :
https://webmail.segrad-center.com/Mondo/lang/sys/client.aspx?CDT=43435.7142577315
Now the code work fine.
I've to upload my project to a2hosting server because I'm using a2hosting server as an email hosting, now everything work fine
I tried 2 Office 365 accounts for SMTP mail sending. One is ok, the other one failed. I am pretty sure the account name and passwords are right.
Error message:
Message = "The SMTP server requires a secure connection or the client
was not authenticated. The server response was: 5.7.57 SMTP; Client
was not authenticated to send anonymous mail during MAIL FROM
[DM3PR11CA0008.namprd11.prod.outlook.com]"
C# code:
int port = 587;
int.TryParse(configuration["smtp:port"], out port);
string host = configuration["smtp:server"] ?? "smtp.office365.com";
SmtpClient client = new SmtpClient(host, port);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(configuration["smtp:username"], configuration["smtp:password"]);
await client.SendMailAsync(mailMessage);
I'm rather new to SMTP and IIS settings but according to the documentation on I've read the web this should be working.
What I'm trying to achieve:
To send an email from the server to a users email using an existing SMTP Relay Server.
What I have done:
In my IIS, for my site (ASP.NET), I have configured the SMTP E-mail.
I have entered:
A random E-mail address (it doesn't have to be an existing, right?)
A SMTP Server IP (in this case an IP to an external SMTP Relay Server)
A port number (25).
Autentication Settings to "Not required".
My method for sending an email looks like this:
public static void SendEmail()
{
var message = new MailMessage()
{
Subject = "Heading",
Body = "Body",
message.From = new MailAddress("test#test.com");
message.To.Add("A valid email address"); //My own email address
}
var smtpClient = new SmtpClient("SMTP-Relay-Server-IP", 25); //Same IP as the one in SMTP E-mail configuration in IIS for the site.
smtpClient.Send(message);
}
}
Facts/questions:
Is this correct? Is it correct to also put the Relay Server IP and Port number in the code as parameters in the new SmtpClient?
I don't get an error but I don't receive an email. (I am 100% sure that the "to-email" is correct.
What can be the reason for this not working? What am I missing or misunderstanding?
Wrap your smtpClient.Send(message); in a try/catch block and log any exceptions that are thrown.
A random E-mail address (it doesn't have to be an existing, right?)
That depends on your SMTP provider and configuration.
Without more information on your SMTP provider or an error message I doubt there's anything we can do for you.
MailMessage mail = new MailMessage("sendTo", "from");
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
mail.Body = "this my message";
smtp.Send(mail);
should be set out like this