avast VS my Programm - c#

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

Related

Why can't I send e-mail from a C# app when I can send e-mail from a mail client just fine?

I have a task to troubleshoot why a C# app is failing to send automated e-mail messages. I carefully checked the source code, and could find absolutely nothing wrong.
Therefore, I tried to send e-mail from Thunderbird, the e-mail client I normally use. I specified the same SMTP relay, the same UID and password, and everything worked fine.
Trying to isolate the problem, I tried writing a very short C# console app to see what might be going wrong. I wrote the following:
using System.Net;
using System.Net.Mail;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
var client = new SmtpClient("my.server.with.ssl", 465);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("uid", "pwd");
var message = new MailMessage("me#example.com", "myemail#mydomain", "Test Message Subject", "Test Message Body");
client.Send(message);
}
}
}
I entered the same credentials that I used in Thunderbird. When I run the above, I get the following error:
Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
How can it be that e-mail is sent just fine from Thunderbird, but the simple programming above fails to work when I try to send an e-mail from a basic C# app?
Edit I tried changing the port number to 587 as suggested in the Question that #stuartd linked to, and in that case, I get an error that says The operation timed out.
Edit I've tried using other e-mail servers and adjusting the settings, but nothing works so far. I've tried connecting to the same SMTP server that I use for my personal e-mail and it shows an error that the connection times out.
Edit I can't say why, but everything seems to be working now in spite of the fact that I didn't change any code. It seems as if something odd happened with my connection.
try using the default constructor and specify the host only.
Also some servers require that the client be authenticated before the server sends e-mail on its behalf. Try changing the value of UseDefaultCredentials
var mail = new MailMessage();
mail.From = new MailAddress("xxx#gmx.net");
mail.To.Add("yyy#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "subject";
mail.Body = "content";
var client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = "mail.gmx.net";
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "pass");
client.Send(mail);
if that doesn't help than tell us the server name if its the public one
Try using:
SmtpClient smtpClient = new SmtpClient("mail.mymailhost.com");
smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Timeout when sending email via SmtpClient

I'm trying to send a very simple email via the SmtpClient class. I've been looking at this for hours and I've been following examples from StackOverflow and MSDN, but for some reason I get nothing but timeouts.
I've changed the hostnames and passwords, but I've added the code below.
static void Main(string[] args)
{
Int32 port = 465;
String host = "host.co.uk";
MailAddress from = new MailAddress("test#email.co.uk");
MailAddress to = new MailAddress("paul#email.co.uk");
MailMessage msg = new MailMessage(from, to);
msg.Body = "Test Body";
msg.Subject = "Test Subject";
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = host;
client.Port = port;
client.Credentials = new System.Net.NetworkCredential("test#email.co.uk", "password");
client.UseDefaultCredentials = false;
client.Send(msg);
}
As you can see, this is a console application that tries to send the email and then exits. I took it out of the main project so I could eliminate any other possible causes (apart from the actual email code).
It throws a System.Net.Mail.SmtpException with the value
The operation has timed out.
What I think is causing the problem is the credentials, because if you look at the values in 'client' it shows the Credentials property as null, despite being set in the code.
I have to admit to being stumped. Thank you in advance for any help you can offer.
Late to the party, but it may help someone...
I had this time out issue occur with a particular SMTP server, using code that had worked for years with numerous other providers (including gmail).
It turns out that Microsoft themselves don't recommend using the SmtpClient class as it doesn't support many modern protocols. They recommend MailKit (NuGet: Install-Package MailKit)as an alternative. MailKit is very similar, code-wise, so easy to switch to. It immediately solved my problem.

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

Code Conversion from Classic ASP to ASP.NET

My client have a classic ASP site on his dedicated server on which he sends mail using IIS (Its working properly there).
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "localhost" ' Specify a valid SMTP server
Mail.Username = "mail#site.com"
Mail.Password = "password"
Mail.From = "info#site.com"
I tried converting this to ASP.NET like this.
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Port = 25;
//smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mail#site.com", "password");
smtp.Send(message);
But this doesn't work. Its almost legacy app without any error logger / monitoring and I cannot debug the code on online server.
What is wrong with my code?
This seems like a permissions issue. Check this thread for details.
You might want to check to see if port 25 is being blocked, as shown in http://kb.siteground.com/article/How_to_check_whether_SMTP_port_25_is_blocked.html
Make sure the pickup directory is being monitored by your local SMTP server if you use this method:
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
If it is not then the messages will remain there without being sent.

Categories

Resources