1

I try two application, one in java and one in C#. The java application can send email successfully but the C# cannot. Here is the two apps : 1.Java

    final String username = "myaccount@mydomain";
final String password = "mypassword";
String smtpHost = "smtp.mydomain";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
});

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(username));
message.setSubject("Test send email");
message.setText("Hi you!");
Transport.send(message);

2.C#

    string username = "myaccount@mydomain";
string password = "mypassword";
string smtpHost = "smtp.mydomain";

SmtpClient mailClient = new SmtpClient(smtpHost, 465);
mailClient.Host = smtpHost;
mailClient.Credentials = new NetworkCredential(username, password);
mailClient.EnableSsl = true;
MailMessage message = new MailMessage(username, username, "test send email", "hi u");

mailClient.Send(message);

So, what is the mistake i made in C# application? Why it cannot send email?

EDIT: I have read this question How can I send emails through SSL SMTP with the .NET Framework? and it works. The deprecated System.Web.Mail.SmtpMail works but System.Net.Mail.SmtpClient doesn't. Why ?

3.This C# code work fine :

    System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","smtp.mydomain");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport","465");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing","2");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myaccount@mydomain");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
myMail.From = "myaccount@mydomain";
myMail.To = "myaccount@mydomain";
myMail.Subject = "new code";
myMail.BodyFormat = System.Web.Mail.MailFormat.Html;
myMail.Body = "new body";
System.Web.Mail.SmtpMail.SmtpServer = "smtp.mydomain:465";
System.Web.Mail.SmtpMail.Send(myMail);
Community
  • 1
  • 1
hieund
  • 356
  • 1
  • 6
  • 16
  • 1
    What kind of error do you get? – SKull Oct 24 '13 at 08:17
  • if i use proxy, it throw "No connection could be made because the target machine actively refused it [serverip]:465". If i don't use proxy, it throw "The operation has timed out.". The java application work on both. – hieund Oct 24 '13 at 08:24

2 Answers2

1

The .NET System.Net.Mail.SmtpClient class cannot handle implicit SSL connection. Implicit SSL connections are sent over Port 465, as the client is configured in your example.

You can use a third party library like AIM (Aegis Implicit Mail) to send implicit SSL messages.

Opal
  • 81,889
  • 28
  • 189
  • 210
Hans Bäuml
  • 229
  • 3
  • 15
0

You can try this code as well. Also sends the email in a seperate thread.

using System.Net.Mail;

public static void Email(string Content, string To, string Subject, List<string> Attach = null, string User = "sender@sender.com", string Password = "MyPassword", string Host = "mail.sender.com", ushort Port = 587, bool SSL = false)
{
    var Task = new System.Threading.Tasks.Task(() =>
    {
        try
        {
            MailMessage Mail = new MailMessage();

            Mail.From = new MailAddress(User);
            Mail.To.Add(To);
            Mail.Subject = Subject;
            Mail.Body = Content;

            if (null != Attach) Attach.ForEach(x => Mail.Attachments.Add(new System.Net.Mail.Attachment(x)));

            SmtpClient Server = new SmtpClient(Host);

            Server.Port = Port;
            Server.Credentials = new System.Net.NetworkCredential(User, Password);
            Server.EnableSsl = SSL;
            Server.Send(Mail);
        }
        catch (Exception e)
        {
            MessageBox.Show("Email send failed.");
        }
    });

    Task.Start();
}
Demir
  • 1,787
  • 1
  • 29
  • 42
  • @Heiund please make sure your host requires SSL (like Gmail) or not. Besides port should be correct. Your host may be using port 25. I've been using this code for very long time without any problem. – Demir Oct 24 '13 at 09:54