Sending emails on behalf of user - c#

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.

Related

Why isnt the mail I sent through SendGrid visible in my sent items? (.net core)

I tried sending mails with the help of send grid mail service using .Net core.
var client = new SendGridClient(xxxxxxxxxxxxxxxxxxxxxx);
var from = new EmailAddress("test#abc.com", "Not User");
var subject = "TestMail";
var to = new EmailAddress("test#abc.com, "Example User");
var plainTextContent = "This is body";
var htmlContent = "";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
client.SendEmailAsync(msg).Wait();
The mails sent using this code are not visible in sent items of my mail box. What is the possible solution to this? Thanks in advance.
When you send the email it uses SMTP directly to sendgrid. When you send via Google they will automatically add it to your outbox. To do the same when someone else sends the message you would have to manually place a copy of the sent message in your outbox using IMAP.
What is the possible solution to this?
You could use SendGrid's BCC option will allow you to BCC an email address, in this case your mail account, with every email sent.
Go to your SendGrid Account>Manage>Settings>Mail Settings and turn on the BCC option.
Note: With this setting turned on, you will be charged an extra email for every email you send. So reclick on Manage and you will see the popup message.
Here is Send mailbox snapshot:

ASP.NET send mail to multiple users

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?

How to send mails to multiple reciepients from web application?

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.

C# smtp response

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.

How can you set the SMTP envelope MAIL FROM using System.Net.Mail?

When you send an email using C# and the System.Net.Mail namespace, you can set the "From" and "Sender" properties on the MailMessage object, but neither of these allows you to make the MAIL FROM and the from address that goes into the DATA section different from each other. MAIL FROM gets set to the "From" property value, and if you set "Sender" it only adds another header field in the DATA section. This results in "From X#Y.COM on behalf of A#B.COM", which is not what you want. Am I missing something?
The use case is controlling the NDR destination for newsletters, etc., that are sent on behalf of someone else.
I am currently using aspNetEmail instead of System.Net.Mail, since it allows me to do this properly (like most other SMTP libraries). With aspNetEmail, this is accomplished using the EmailMessage.ReversePath property.
MailMessage.Sender will always insert a Sender header (interpreted as on behalf of in your e-mail client).
If you use the Network delivery method on the SmtpClient, .Sender will also change the sender in the envelope. Using the PickupDirectoryFromIis delivery method will leave it to IIS to determine the envelope sender, and IIS will use the From address, not the Sender address.
There's a similar question on MSDN here.
I just found how to do it:
mail.From specify the email from visible to the final user
mail.Sender specifies the envelope MAIL FROM
That's it (even if it took me a while to figure it out)
If you add the following lines the Return-Path and the Reply-To headers are set in the mail header.
Dim strReplyTo As String = "email#domain.tld"
message.ReplyToList.Add(strReplyTo)
message.Headers.Add("Return-Path", strReplyTo)
And if you click on reply the e-mail set to the Reply-To address
Do you mean this?:
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
From http://www.systemnetmail.com/faq/3.1.2.aspx

Categories

Resources