I am trying to develop a email client. which sends email to the given recipients
using System.Net;
using System.Net.Mail;
MailMessage msg;
SmtpClient client;
SMTPURL=abc.xyz
SMTPPort=87
client = new SmtpClient(SMTPURL, SMTPPort);
client.Credentials = new NetworkCredential(senderID, senderPWD);
msg = new MailMessage();
msg.To.Add("rx#gmail.com");
msg.Body="hello hi bye";
client.Send(msg);
this code is working well, but I have a backup email server with URL 123.xyz
if my abc.xyz is down or I have wrong url I will get a SMTPException
Now my question is how to reroute my message to 123.xyz backup mail server
My assumption is to catch the SMTPException and change the SMTPURL to 123.xyz and resend, but is this a good way or any other alternates exists to reroute to secondary mail server ?
Thanks in advance
you should be able to use your basic try/catch block:
public void function sendemail()
{
try{
SendEmailByServer(primaryserverurl);
}
catch(SMTPException se)
{
sendemailbyserver(backupurl);
}
catch(Exception ex)
{
//something else broke
}
}
public void function SendEmailByServer(string server)
{
MailMessage msg;
SmtpClient client;
SMTPURL=server;
SMTPPort=87;
client = new SmtpClient(SMTPURL, SMTPPort);
client.Credentials = new NetworkCredential(senderID, senderPWD);
msg = new MailMessage();
msg.To.Add("rx#gmail.com");
msg.Body="hello hi bye";
client.Send(msg);
}
Related
I have a piece of code sending out account activation emails with a link in it using SMTP. My code connects to my mail box on an email provider and sends out mails. The first few mails went through. And then they started failing. Obviously they were blocked as spams.
My question is then how can I test my code? People suggest to alter the configurations of the mail server. But since I am using a 3rd party email provider, I have no control over it.
My website production server is on AWS but I can't use that for my testing.
Here is a snippet of my code. Pretty standard.
using (var msg = new MailMessage())
{
msg.From = new MailAddress(From);
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
msg.To.Add(toEmail);
string error = "";
try
{
using (var client = new SmtpClient(SMTPServer))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);
client.Send(msg);
}
}
catch (SmtpFailedRecipientException se)
{
error = $"Unable to mail to {toEmail}";
}
catch (SmtpException se)
{
error = "Mail server connection failed.";
}
catch (Exception ex)
{
error = "Email failed";
}
return error;
}
SmtpException is thrown and no mails are sent/received.
The Subject is Blah blah Account Activation and the Body is Please use the following link to activate your account: <a href='blah blah blah'></a>.
I have a list of data from a database and I have Email Id's in database, i have to send email to those ID's where if its retention date is tomorrow means i have to send an intimation email as reminder by today,i want to send email, and i will use service to send it daily, but my email part is not working..below is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace SendingEmail
{
public class SendingMail
{
public static void SendMail(string recipient, string subject, string
body, string attachmentFilename)
{
//method to send email
MailMessage mail = new MailMessage();
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "my password");
string From = "my email#gmail.com";
string To = "email.com";
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
MailMessage msg = new MailMessage(From, To);
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment
(#"C:\Users\rahul.chakrabarty\Desktop\logg.txt");
mail.Attachments.Add(attachment);
SmtpServer.Send(mail);
}
//To cathch Exception
catch (Exception ex)
{
Console.WriteLine("unable to send" + ex);
}
}
}
}
My error is "unable to sendsystem.InvalidOperationException: A from address must be specified"..This is my error
You said
String from = "my email#gmail...
But nowhere have you actually assigned this string to the mail.From property - you've gone and made a new MailMessage using that from address, and called it msg:
MailMessage msg = new MailMessage(From, To)
but you aren't sending the msg mail, you're sending the mail mail:
SmtpServer.Send(mail);
The same problem exists with the To address on the mail variable
Basically, the code is all messed up: you make TWO MailMessage objects, set half the necessary things on one, the other half of necessary things on the other, and then try to send one of them with incomplete details. It's kinda like you copied and pasted two different tutorials together but didn't get enough of either of them to get a complete MailMessage ready for sending
I could have fixed this all up for you and posted working code, but I haven't for two reasons: 1) it's very easy to do these small changes yourself, and 2) I want YOU to do it as a learning exercise rather than just giving you the answer :)
Make sure you don't put a space in the email address either
I am trying to make a Windows Form that sends an email when the user clicks a button, but every time I try, an exception is being raised: Failure sending mail. What is wrong with my code?
private void button1_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("username", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("myemailadress#gmail.com");
smtpClient.Host = "smpt.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("towhomisend#yahoo.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
MessageBox.Show(ex.Message);
}
}
use "smtp.gmail.com" instead of "smpt.gmail.com"
1) Check whether SMTP settings are correct and server is configured correctly (Host & Port Settings)
2)Check whether credentials(user name and password) are correct
3)Check whether firewall is blocking the request
4).Check port 587 If it is blocked in firewall
Port 587:
This is the default mail submission port. When a mail client or server is submitting an email to be routed by a proper mail server, it should always use this port.
The 'smtpClient.Host = "smpt.gmail.com";' part is incorrect.
Change "smpt.gmail.com" to "smtp.gmail.com"
"S M T P" - not - "S M P T"
use "smtp.gmail.com" instead of "smpt.gmail.com"
That aside
Confirm one more thing, You have to enable email from other program in your gmail.
When you try to send once, you will get a notification in you email ID.
You have to enable it there.
This is my first post on Stack, and I've literally just started programming, so please, be patient.
I'm attempting to send an email to "x" email address when Button 1 is pressed. I've looked around, and every thread is jargon-heavy and not in my context. Please pardon me if this is a "newb' question, or if it's already thouroughly answered somewhere else.
The error I'm getting is "
Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed."
Here's my code
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("myemail", "mypassword");
string to = "toemail";
string from = "myemail";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "test test 123";
mail.Body = "test test 123";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
Any help is greatly appreciated!
The error you're getting could come from a plethora of things from a failed connection, to the server rejecting your attempts, to the port being blocked.
I am by no means an expert on SMTP and its workings, however, it would appear you are missing setting some of the properties of the SmtpClient.
I also found that using port 465 is a bit antiquated, and when I ran the code using port 587, it executed without an issue. Try changing your code to something similar to this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static void SendMail()
{
MailAddress ma_from = new MailAddress("senderEmail#email", "Name");
MailAddress ma_to = new MailAddress("targetEmail#email", "Name");
string s_password = "accountPassword";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
//change the port to prt 587. This seems to be the standard for Google smtp transmissions.
Port = 587,
//enable SSL to be true, otherwise it will get kicked back by the Google server.
EnableSsl = true,
//The following properties need set as well
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
try
{
Console.WriteLine("Sending Mail");
smtp.Send(mail);
Console.WriteLine("Mail Sent");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
Console.ReadLine();
}
}
}
}
Tested to work as well. Also of note: if your Gmail account does not allow "Less Secure" apps to access it, you'll get an error and message sent to your inbox stating an unauthorized access attempt was caught.
To change those settings, go here.
Hope this helps, let me know how it works out.
how can i send mass mail through bcc in asp.net(c#)..??
According to the documentation you do it like this:
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
You just add several bcc addresses. Note that sending mass mails will easily get you blacklisted and most of your messages will probably end up in spam folders. You should consider using a mass mail service provider for this instead.
You can make you of the System.Net.Mail namespace classes for send out mails in .net applications. Refer to MSDN documentation: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx