Recently I started working on sending an email from an ASP.Net MVC application, while checking on the internet everybody is giving the base code for sending an email. I was trying to use smtp.google.com as my SMTP server with port 587. I was always getting an error server actively rejected my connection. Nobody is giving concrete information on enabling settings on gmail account settings. I used to get an email from Gmail saying un-authorized access to my account is blocked. And the email has a link to allow less secure app settings, which allows us to login from different devices and applications using our own settings. You need to enable to send email from your application.
Here is the URL for gmail settings: Gmail Settings
Also verify settings for Antivirus installed on your machine. By default, it will block the connection. You can verify the same in Event Viewer -> Application logs - look for any Warning/Information messages related to this connection. You may find some thing like this:
Blocked by port blocking rule (Anti-virus Standard Protection:Prevent mass mailing worms from sending mail). And a warning message showing the same message as the exception you got from the code.
You need to uncheck the setting for "Prevent mass mailing worms from sending mail" in you Anti-virus software. Here is the snapshot for the same for McAfee.
Please refer to anti-virus settings as the settings may differ based on the Anti-virus installed on your machine. If Anti-virus is blocking the connection you may the following exception form your code
Here is an example code I used to send email using Google SMTP server after enabling my Gmail settings for my account and updating Anti-virus settings on my machine.
Also verify settings for Antivirus installed on your machine. By default, it will block the connection. You can verify the same in Event Viewer -> Application logs - look for any Warning/Information messages related to this connection. You may find some thing like this:
Blocked by port blocking rule (Anti-virus Standard Protection:Prevent mass mailing worms from sending mail). And a warning message showing the same message as the exception you got from the code.
You need to uncheck the setting for "Prevent mass mailing worms from sending mail" in you Anti-virus software. Here is the snapshot for the same for McAfee.
Please refer to anti-virus settings as the settings may differ based on the Anti-virus installed on your machine. If Anti-virus is blocking the connection you may the following exception form your code
No connection could be made because the target machine actively refused it 74.125.207.109:587
If Gmail setting is disabled you may get the following exception from your codeThe SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Here is an example code I used to send email using Google SMTP server after enabling my Gmail settings for my account and updating Anti-virus settings on my machine.
To send content, you need to use AlternativeView.
Namespace: System.Net.Mail
Create mail message:
var mailMessage = new MailMessage
{
From = new MailAddress("user@gmail.com", "firstname, lastname"),
Subject = "Test message"
};
// add TO list
mailMessage.To.Add(new MailAddress("user1@gmail.com", "user1-display-name-is-optional"));
mailMessage.To.Add(new MailAddress("user2@gmail.com", "user2-display-name-is-optional"));
// add CC list
mailMessage.CC.Add(new MailAddress("user3@gmail.com", "user3-display-name-is-optional"));
mailMessage.CC.Add(new MailAddress("user4@gmail.com", "user4-display-name-is-optional"));
// add Bcc list
mailMessage.Bcc.Add(new MailAddress("user5@gmail.com", "user5-display-name-is-optional"));
mailMessage.Bcc.Add(new MailAddress("user6@gmail.com", "user6-display-name-is-optional"));
Create SMTP Client:
var gmailClient = new SmtpClient{
Host = "smtp.gmail.com",
Port = 587,
Credentials = new NetworkCredential("user1@gmail.com", "password"), // user1 is same as From mail
EnableSsl = true
};
To Send HTML Content:
var htmlText = "<h3>This is html text</h3> <br />Hello<br />" +
"<br /><img src=\"cid:myImageId\"/>";
var htmlContent = AlternateView.CreateAlternateViewFromString(htmlText, null, MediaTypeNames.Text.Html);
// TO linking our image as an embedded resource
var myPicture = new LinkedResource("G:\\Temp\\Capture.JPG", MediaTypeNames.Image.Jpeg)
{
ContentId = "myImageId"
};
htmlContent.LinkedResources.Add(myPicture); // Linking the image
mailMessage.AlternateViews.Add(htmlContent);
To Send Plain Text:
mailMessage.AlternateViews.Add(normalText);
This comment has been removed by the author.
ReplyDeleteThis is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
ReplyDeleteData Science Training in Chennai | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm
Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
ReplyDeleteonline Python certification course
python training in OMR
python training course in chennai
Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
ReplyDeleteAWS Training in Bangalore | Amazon Web Services Training in Bangalore
AWS Interview Questions And Answers
Learn Amazon Web Services Tutorial |AWS Tutorials For Beginners
Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
AWS Training in Chennai |Best Amazon Web Services Training in Chennai
AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli
AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar
AWS Training in Chennai |Best Amazon Web Services Training in Chennai
Happy to read this blog very nice, in this technology world
ReplyDeleteAws training chennai | AWS course in chennai
manual testing course chennai | Manual testing class chennai
Thanks For Sharing the Articles,just Thanks Is not enough for all Keep Updating The Articles looking towards more
ReplyDeleteTo Learn Python and get a Consequence with Happy Then visit same
python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery
Thank you for sharing .The data that you provided in the blog is informative and effective. keep to share!!
ReplyDeleteAndroid Training in Chennai
Android Online Training in Chennai
Android Training in Bangalore
Android Training in Hyderabad
Android Training in Coimbatore
Android Training
Android Online Training
perde modelleri
ReplyDeleteNumara onay
mobil ödeme bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
TRAFİK SİGORTASİ
dedektör
web sitesi kurma
Ask Kitaplari
smm panel
ReplyDeletesmm panel
iş ilanları
İnstagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
Servis
Tiktok hile indir
kartal mitsubishi klima servisi
ReplyDeleteümraniye mitsubishi klima servisi
beykoz vestel klima servisi
beykoz bosch klima servisi
beykoz arçelik klima servisi
pendik mitsubishi klima servisi
ataşehir samsung klima servisi
çekmeköy mitsubishi klima servisi
ataşehir mitsubishi klima servisi