Sending an SMTP email through GMail with reply support - c#

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.

Related

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.

cancel Send mail using smtp SMTP

In my application, I'm using SmtpClient for sending emails(only from gmail accounts). everything is fine and perfect.
I Saw an option in gmail that allows us to cancel/undo the send mail. as shown in the below image.Here my question is regarding that, how can i cancel/undo the send mail from my program based on some conditions.
following is the code i'am using for sending mail:
MailMessage mail = new MailMessage("frommail#company.net", "mail#gmail.com");
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("frommail#company.net", "xxxxxxx");
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
// this is what i want to do
if(someCondition=true)
{//undo the send operation}
From my limited knowledge, you cannot cancel a mail that was sent to a mail server. You can accomplish the undo functionality by:
Save the mail details in a table (lets call it pending_emails) on your server
A (delayed job is scheduled) or (recurring job runs every X minutes) and queries pending_emails for entries made 5 minutes earlier and sends them to the smtp server (as you are doing now)
The job sends the email and deletes the email from pending_emails
The undo button deletes the email from pending_emails
Google just delays the send of the email by 30 seconds, easiest way to do it yourself is an async timer of 30 seconds before sending it via smtp or using a filter before sending.

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.

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.

Delivery Notification to alternate email address

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);

Categories

Resources