SMTP mail only sends internally - c#

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.

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.

avast VS my Programm

I have problem with my program. I make a simple mail-messenger, and in this code :
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.mail.ru");
smtpServer.Port = 25;
smtpServer.Credentials = new
NetworkCredential("#mail.ru", "pass");
smtpServer.EnableSsl = true;
mail.Body = text;
mail.From = new MailAddress("#mail.ru");
mail.To.Add("#mail.ru");
smtpServer.Send(mail); // in this moment
Avast find the idp generic in moment of mail send. The other antiviruses (eset32, Kaspersky,cureit) do not see problems and danger in code. I also was try to send mail with mailkit.dll. how can I fix it?
Your best bet is probably using the standard SMTP client-to-server email submission port: 587.
smtpServer.Port = 587;
If you, for whatever reason, can't use the standard email submission port, use the Avast GUI to make exceptions. Doing it through the antivirus directly should allow your program to run on your machine, and that's all you want, right? ;)
You can try:
Excluding your build directory from live scans
Authorizing your process to use port 25
Avast help pages for further reading

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.

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

Categories

Resources