cancel Send mail using smtp SMTP - c#

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.

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

Issue in receiving emails [duplicate]

This question already has answers here:
The SMTP server requires a secure connection or the client was not authenticated
(3 answers)
Closed 8 years ago.
I am doing my project in mvc4 using c#. I have a contact page i my website. My need is that i have to receive messages to my email id from other id's, when clicking the Send button.I use the following code
public void ReceiveMail(string name,string email,string message)
{
MailMessage msg = new MailMessage();
HttpContext ctx = HttpContext.Current;
msg.To.Add(new MailAddress("MyEmailId"));
msg.From = new MailAddress(email);
msg.Subject =name + "send a message";
msg.Priority = MailPriority.High;
msg.Body = message;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");// i am confused what to write here
SmtpServer.Send(msg);
}
It shows the error
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.7.0 Must issue a STARTTLS command first.
at4sm42219747pbc.30 - gsmtp
I don't know from which server i got the mail. Then how can i solve this issue . Please help me
Sending emails with Gmail requires some additional settings. At first, port number should be 587 (instead of default 25). At second, Gmail requires secure connection. And of course you should provide valid credentials.
All in all, initialization of SmtpClient should look like this:
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential("username#gmail.com", "password");
as the error says, a STARTTLS command should be used first. Thas means gmail only accepts mail via secure connection. In this answer enableSsl was set to true. As the documentation from microsoft says, the SmtpClient class has such an property too. Furthermore you should leave your credentials in the smptClient. I think gmail only accepts mail from authenticate users. I think the whole problem is solved here.
You need to use NetworkCredential to login into Gmail SMTP server. Error is very apparent.
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("your-email", "your-password");
Have you tried:
smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials =
new NetworkCredential("SenderGmailUserName", "SenderPassword");

Categories

Resources