I have developed a asp.net Mvc 4 project and now i am planing to integrate a Mail system in my application.Initially i taught like integrating mail System in my existing web application but later i moved it to a console application using Scheduler to send mail at some time interval.
My scenario is like i have a list of mail ids and i need to send mail to all these mail ids . I have checked System.Web.Mail and i found i can only give one email address at a time. Is it possible in System.Web.Mail or is there any other library available to achieve my scenario.
To in System.Net.Mail is a MailAddressCollection,so you can add how many addresses you need.
MailMessage msg = new MailMessage();
msg.To.Add(...);
msg.To.Add(...);
Chris's answer is correct but you may want to also consider using a mail service. Here are some you could try - they all have a free tier to get started on.
http://sendgrid.com/
http://www.mailgun.com/
https://mandrill.com/
http://aws.amazon.com/ses/
You can easily sent emails to more than one recipient. Here is a sample that uses a SMTP server to send an email to multiple addreses:
//using System.Net.Mail;
public void SendEmail(){
MailMessage email = new MailMessage();
email.To.Add("first#email.com");
email.To.Add("second#email.com");
email.To.Add("third#email.com");
email.From = new MailAddress("me#email.com");
string smtpHost = "your.SMTP.host";
int smtpPort = 25;
using(SmtpClient mailClient = new SmtpClient(smtpHost, smtpPort)){
mailClient.Send(email);
}
}
Just a note: if you go with SMTP, you should probably have a look also on MSDN for the SmtpClient.Send method, just to be sure you are catching any related exceptions.
Related
Hello i have a problem with sendig mails from asp.net web site. Here is my code
private void mailgonder(string mail, string adsoyad)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress("my mail address", "my name");
mm.Subject = "YENİ DUYURU";
mm.Body = "Sistemde okumadığınız yeni bir duyuru bulunmaktadır.";
mm.To.Add(new MailAddress(mail, adsoyad));
SmtpClient sc = new SmtpClient("my smtp client");
sc.Port = 587;
sc.Credentials = new NetworkCredential("my username", "my password");
sc.Send(mm);
}
I want to send mail to 1500 users with same time. How can i do this in asp.net (we will have over 1000 member in this web project)
I can send like this to one person. But i don't know how can i send this to multiple persons
Thanks
As MailMessage.To is a collection, you can add as much as you want :
mm.To.Add(new MailAddress("user#user.com", "user1"));
mm.To.Add(new MailAddress("user1#user.com", "user2"));
mm.To.Add(new MailAddress("user2#user.com", "user3"));
mm.To.Add(new MailAddress("user3#user.com", "user4"));
.....
until
mm.To.Add(new MailAddress("user1499#user.com", "user1499"));
mm.To.Add(new MailAddress("user1500#user.com", "user1500"));
NOTE :
Restrictions may exist according to your mail server or access provider. For more efficiency, use a mailing list.
You have to send each user seperate emails. Do not, never, add more than 1 address to TO unless you have sender score certification, doing opposite will cause you get banned from mail providers since they would think you are a spammer. And also spam list is global, which means if you get banned from gmail, you will also get banned from hotmail or yahoo in a short period of time.
Just select for example 20 email addresses from database each time and loop that 20 and call your mailgonder method for each.
Other than what I wrote above, if you write multiple TO's, each of your users will see each others email adressses which is not cool from user perspective.
I Believe the simplest approach by far is to make a contact group. Doing this will keep the amount of code written to a minimum. Here is a link explaining how to do so in Outlook How to make a contact group
Once you have the contact group setup, simply do this:
String Devemail = "devgroup#whatever.com";
MailMessage message = new MailMessage();
message.To.Add(Devemail);
//The rest of your code here
The rest of your code will work fine.
If someone has a better/simpler approach, please feel free to correct me.
EDIT: I am kind of shocked that we recommend he hard code 1500 email addresses into his code. Can someone give me a good reason for using that approach over using an email group?
So im implementing a mailservice in our webapp that users can utilize to send out emails to persons on a certain list. Now i need to know how to best use the System.Net.Mail object to send mail on the users behalf. Ive been trying this now for a while without the right results.
I would like the mail to read "our system on behalf of user 1" and the reply to adress should be the adress that user1 has in our system so that when the contacted person wants to reply to the mail he should get user1:s address, not ours. How can I do this?
Which fields do I need to declare and how? This is how i have it set up right now, but a reply to these settings sends a mail back to noreply#oursystem.com
from = 'noreply#oursystem.com'
replyTo = 'user1#privateaddress.com'
to = 'user1#privateaddress.com'
sender = 'user1#privateaddress.com'
cc = ''
ReplyTo is obsolete. You should use ReplyToList
Example:
MailAddress mailFrom = new MailAddress("noreply#oursystem.com");
MailAddress mailTo = new MailAddress("user1#privateaddress.com");
MailAddress mailReplyTo = new MailAddress("user1#privateaddress.com");
MailMessage message = new MailMessage();
message.From = mailFrom;
message.To.Add(mailTo); //here you could add multiple recepients
message.ReplyToList.Add(mailReplyTo); //here you could add multiple replyTo adresses
This was caused by a programming error on the receiving end. The correct solution is to set the from and replyto as above. That will get the correct behaviour.
i have few question about smtp
im using this code to send mails if the host is gmail then it act diffrente:
foreach (string host in hosts)
{
SmtpClient sc = null;
try
{
if (emailDomain.ToLower() == "gmail.com")
{
MailSend.MailSendApp.EventLog.WriteEntry("mail to gmail.com");
sc = new SmtpClient("smtp.gmail.com", 587);
sc.UseDefaultCredentials = false;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = new NetworkCredential("UID#gmail.com", "PWD");
sc.EnableSsl = true;
} }
else
{
sc = new SmtpClient(host);
sc.Send(mailMessage);
break;
}
is it possiable to get answer from smtp :
1. that the email arrived
2. if the mail exists
thanks
If you want to receive a notification that the email has arrived you need to send the email with delivery notification options.
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
If the email doesn't exists, you will get an email back to your sender address and not to your SMTP class.
In short, there's no easy way to determine these two thing purely from the SMTP class perspective.
Back in the day you could directly query whether the email address exists on a given server (VRFY user SMTP command). Then, SPAM was born and pretty much all email servers removed that capability because spammers would use bots to query possible email recipients on each mail server to build SPAM lists.
You will still get an ordinary bounce report (email back to the reply-to address indicating that delivery failed). I use a tool called Boogie Tools to automate processing of bounce messages.
Gmail does not offer delivery or read receipts (though some email servers still optionally allow it) for similar reasons... too much abuse potential.
I have a small email application that lets a user build a message from checkboxes and then send the message as an email. I am trying to add an image to the email, i.e. a logo or signature. The application worked fine but as I was reseaching getting the images into the email, I found that I should be using System.Net.Mail instead of Interop. So I changed my email class to the code below. Now I'm not recieving the emails. I'm assumeing this is because the code is set-up for a server and I just want to run this on my local machine. This is just something I'm playing with to help me understand some concepts so real world use is not going to be a factor. I just want to be able to test my little program on my Outlook email account locally. My code is as follows...
using System;
using System.Net.Mail;
using System.Net.Mime;
namespace Email_Notifier
{
public class EmailSender:Notification
{
string emailRecipient = System.Configuration.ConfigurationManager.AppSettings ["emailRecipient"];
public void SendMail(string message)
{
try
{
string strMailContent = message;
string fromAddress = "MyApp#gmail.com";
string toAddress = emailRecipient;
string contentId = "image1";
string path = (#"C:Libraries/Pictures/Logo.gif");
MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
mailMessage.Subject = "Email Notification";
LinkedResource logo = new LinkedResource( path , MediaTypeNames.Image.Gif);
logo.ContentId = "Logo";
// HTML formatting for logo
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:Logo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("localhost");
mailSender.Send(mailMessage);
}
catch (Exception e)
{
Console.WriteLine("Problem with email execution. Exception caught: ", e);
}
return;
}
}
}
You specify mailSender = new SmtpClient("localhost");.
Have you set up an SMTP server on your local machine? If not, you will need to do so to use SmtpClient. Otherwise, specify a hostname other than localhost, perhaps using your Gmail account as specified here, bearing in mind you will need to configure it for authentication and SSL.
See also the documentation for SmtpClient.
There are also one or two other issues I can see with your code - but let's deal with these after getting simple mail sending working.
if your problem is that you don't have a smtp server you are talking to on your machine, you will see an exception rather than just silently not having your email delivered (which i believe is what you are seeing given that you did not list an exception, but not completely sure). it could be that you are successfully delivering to your local smtp server, but it is failing to send the mail on. if you have the iis smtp server installed and that is what you are trying to use to send the mail, you can see failed email messages in the subdirectories of C:\Inetpub\mailroot. there is a badmail subdirectory that should have failed deliveries.
if you are on windows 7, it doesn't support the iis smtp server. an alternative for having an smtp server on your machine that doesn't actually deliver the email but does show you everything that has come through for any email address is http://smtp4dev.codeplex.com/. even if i do have the iis smtp server available in my operating system, i prefer this for development needs.
I am writing a windows service that will be doing a lot of network communication (copy many many files in shared folders and modify database).
I need a way to notify a user (if logged on) of any exceptions/errors. My issue is do group to errors for send it (by email) to administrator address.
I am aware of event logging neither notification bubble from system tray, but the administrator not views that log, he prefers email.
The ideal component is ASP.NET Health Monitoring, but only works for IIS, and it is required a component similar for Windows Services.
Any sample application Windows Service in C# or another language in .net (with source code) about this issue ??
If you're just needing a way to send an email notification, .Net has SMTP and mail types to take care of this. Email notifications are common in software anymore and that's why the commonly used functionality has been incorporated into the base class lib.
You could use SmtpClient, MailAddress, and MailMessage objects to accomplish what you need to simply send an email notification. Of course you need to have access to an SMTP server to transmit the mail, so find out what its host address is to properly configure your application. Here are a few examples:
SmtpClient mailClient = new SmtpClient("smtp.fu.bar");
MailAddress senderAddr = new MailAddress("you#fu.bar");
MailAddress recipAddr = new MailAddress("admin#fu.bar");
MailMessage emailMsg = new MailMessage( senderAddr, recipAddr );
emailMsg.Subject = "Test email.";
emailMsg.Body = "Here is my email string which serves as the body.\n\nSincerely,\nMe";
mailClient.Send( emailMsg );
That example is just straight code, but it would be better to put it into a reusable method like this:
public void SendNotification( string smtpHost, string recipientAddress, string senderAddress, string message, string subject )
{
SmtpClient mailClient = new SmtpClient(smtpHost);
MailMessage emailMsg = new MailMessage( new MailAddress(senderAddress), new MailAddress(recipientAddress) );
emailMsg.Subject = subject;
emailMsg.Body = message;
mailClient.Send( emailMsg );
}
check this : http://www.codeproject.com/Articles/16335/Simple-Windows-Service-which-sends-auto-Email-aler
my advice if it's first time to work with windows service (because Windows Service is very hard to debug)
first make it Command Line application with c#
when you be certain with all functionality of your project move it to Windows Service