Delivery Notification to alternate email address - c#

I'm trying to write a simple form that will send system emails through SMTP to various users they select from a generic email account in an ASP.NET MVC3 web application. This account is a gmail account currently but is probably going to change to another domain once we publish this project.
Is there a way to send a delivery failure notification email to any email address other than the generic email account that is used to send the emails that is available in ASP.NET?
Here's the code I've used. I've already tried a few of the solutions that involve headers but am not getting much success. For the example, the email address I'm trying to send the delivery failure notification emails is test#email.com (the actual address to use would be pulled from the user's login)
MailMessage mail = new MailMessage();
mail.From = new MailAddress(ConfigurationManager.AppSettings["Mailer.Smtp.User"], "Mailer");
mail.To.Add(to);
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = true; // to incorporate the editor into it
mail.ReplyToList.Add(new MailAddress(From));
mail.ReplyTo = new MailAddress(From);
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.Headers.Add("Read-Receipt-To", "test#email.com");
mail.Headers.Add("Disposition-Notification-To", "test#email.com");
SmtpServer.Send(mail);

Related

Sending an SMTP email through GMail with reply support

I am creating a gmail SMTP client like so:
client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(email, password);
client.Timeout = 20000;
And sending an email like so:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(myemail-no-reply#gmail.com);
msg.To.Add(to);
msg.Subject = subject;
msg.Body = body;
client.Send(msg);
I wish that if someone replies to my email it'll go to the admins email address, not to my application, and also ideally for them not to even know about the existence of the application's email address.
I could have my application forward emails received to the admin, but that's an ugly solution.
I understood from other threads that there's no way to send an email with a "reply to" field.
What I'm wondering is what if I have the admin's email as a "Send mail as" registered account on the application's GMail account.
So through the GMail interface I can send an email as if I'm the admin, can I do this from my c# application?
I know that I can set the from field of the msg sent to the admin's email, but I'm wondering if this is the correct approach, if gmail supports this, and if the email doesn't have more likelihood of ending up in the spam folder.
I have given authority to "Send mail as" in the application e-mail as the admin, and simply editing the "from" field to be sent as the admin's email address appears to be working flawlessly.
So I guess this is one of those cases that the simplest solution is the correct one.
Edit: It appears that editing the "from" field doesn't allow you to send from a different address, but what does work is setting up the default email to send emails from as a different email in the GMail "send mail as" options.

How to send message to hotmail using asp.net without user password?

I have a web page, In CONTACT US tab I have a forms user can only enter their Name, Email Id, Subject and message. Once they click the ok button I want to get those message to my hotmail account.
I tried some code. But it doesn't work.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail.text, txtPassword);
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject = "Demo";
msg.Body = "Hi there..";
string toAddress = "xxx#hotmail.com"; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"no reply \" <from#gmail.com>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try
{
smtp.Send(msg);
}
catch
{
throw;
}
This code I tried. But it's having a password. I want user to send email without password to my xxx#hotmail.com
Here is my form design
That depends on your host. Usually web hosts give you a local SMTP server, then you can use it to send any mail you want, just need to know the configuration settings and use them with the SmtpClient.
If your host doesn't offer smtp (very strange unless you're selfhosting the page) you can:
1-Install a local SMTP server (if you manage the server), this is the preferred solution.
2-Use an external service like google to send the mails, but then you need to create an account on the service and use these credentials, and have in account that Google has a lot of restrictions sending emails (limit per second, marking mails as spam, etc etc).
Preferred way to do this is to not have the mail be sent from the users e-mail, but rather have a dummy e-mail that sends the mails and contains the data the user entered. Not the best solution probably, but it doesn't require user credentials.

SMTP mail only sends internally

I've got a c# function that sends emails from the win app to me via SMTP from a gmail account.
It works, but only if the FROM parameter is internal.
When I add an external address, it runs without errors, but the email never arrives in my mailbox.
I've noticed several posts on this site with similar issues, but the difference with most seems that everyone else gets some kind of error message.
MailMessage mail = new MailMessage();
using (SmtpClient SmtpServer = new SmtpClient(smtp.gmail.com))
{
mail.From = new MailAddress("you#myGoogleDomain.com"); //This works
//mail.From = new MailAddress("me#somewhere.com"); //This does not work
mail.To.Add("recipient#myGoogleDomain.com");
mail.Subject = "Mail Subject";
mail.Body = mailBody;
mail.ReplyTo = new MailAddress("me#myGoogleDomain.com");
mail.CC.Add(new MailAddress("me#myGoogleDomain.com"));
mail.IsBodyHtml = true;
SmtpServer.Port = 587; //I've also tried 465
//SmtpServer.UseDefaultCredentials = false; //This does not make a difference.
SmtpServer.Credentials = new System.Net.NetworkCredential("myaccount#myGoogleDomain.com", "Pass123");
SmtpServer.EnableSsl = true; //I've also tried false
//Add attachments
...
//Send the mail
SmtpServer.Send(mail);
}
MORE INFORMATION: I notice that the emails sits in the myaccount#myGoogleDomain.com 'Sent' box. Just no mails coming through to recipient#myGoogleDomain.com.
You are sending email using Google smtp server on behalf of your account in Google. You can't use Google smtp with non-google account to From. Even if you are sending regular email in Gmail or other email you can't modify From. Same behavior in this code you posted.
The problem was not really with the From param, but with the ReplyTo. Since both params are set from the same variable (and not strings), it did not work when it was set to outside my domain.
Removing the ReplyTo settings solved the problem.

Send data file via email using C#

is it possible to send via email the file or data of an application made using c#?
i have a program which will have its data stored in sqlite database at appdata. so i need to back it up regularly (or everyday) in case of accidental deletion of the data without manually sending it through internet.
If it is possible, can you help me with it? like posting here links or tuts on how to.. answers are very much appreciated.
The program create the file database.sqlite at the AppData/MyProgram folder and i want to send that file.
I write a simple guide to do what u want.
Look at the SmtpClient class and MailMessage class.
You need to dump the data to a file or u can attach the sqlite file itself, as an attachment for the email.
then you can use a SMTP server to send emails, take a look at this question : Sending email in .NET through Gmail
You can send email with your file attached as attachment using .Net mail class.
Below is code that send email with attachment.
var smtp = new System.Net.Mail.SmtpClient();
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(sFromEmail);
string sFrom = mail.From.ToString();
mail.Subject = sSubject;
mail.Body = sBody;
mail.IsBodyHtml = true;
Attachment sMailAttachment;
sMailAttachment = new Attachment("Your file file");
mail.Attachments.Add(sMailAttachment);
smtp.Host = "SMTPP HOST"
smtp.Port = "PORT"
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(sSMTPUserName, sSMTPPassword);
smtp.Timeout = 30000;
smtp.Send(mail);
}
There are a bunch of articles (and StackOverflow posts) on how to send email using C#, just do an internet search on "send email c#" this post can get you started.
The only thing you really need to make sure of, is that you have an smtp server (outgoing mail server) that you have permission to send through. Usually, that means the ourgoing mail server of your company of internet service provider, but that would need checking as it tends to differ for everyone.
Also: be aware that most (if not all) "regular" smtp servers will have a cap on the amount of emails per minute or hour you can send, so don't overdo it.
public void mail()
{
MailMessage sendmsg = new MailMessage("frommail", "tomail", "subject", "body");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt16("587");
client.Credentials = new System.Net.NetworkCredential("frommail", "password");
Attachment sMailAttachment;
sMailAttachment = new Attachment("your file");
sendmsg.Attachments.Add(sMailAttachment);
client.EnableSsl = true;
client.Send(sendmsg);
}

Unable to Send mail from Gmail Account in ASP.NET

I am sending mail using this code
using System.Net.Mail;
using System.Net.Security;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#gmail.com");
mail.To.Add("xyz#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Mail Done";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Sent";
Whem I am using abc#gmail.com(one email id) for sending email, mail will successfully send but when I am using pqr#gmail.com(another mail id) mail sending failed. On local server both "abc" & "pqr" working fine.
Please help me to sort out this problem.
Error Message
the smtp server requires a secure connection or the client is not authenticated the server response was 5.5.1 authentication requires
Update you code to the following:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Try this line after the enableSSl=true code
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
Gmail has added a new security feature for not to allow sending email using the less secure apps. You have to change the settings for your gmail account to allow access by less secure apps using steps given by google at below link
https://support.google.com/a/answer/6260879?hl=en
Enable less secure apps to access accounts
1.Sign in to your Google Admin console.
Click Security > Basic settings.
Under Less secure apps, select Go to settings for less secure apps.
In the subwindow, select the Allow users to manage their access to less secure apps radio button.
Once you've set Allow users to manage their access to less secure apps to on, affected users within the selected group or Organizational Unit will be able to toggle access for less secure apps on or off themselves.

Categories

Resources