Reputation: 2817
I'm building a windows forms application that's supposed to run on a remote/isolated machine and send error notifications by email to the admins. I've tried employing System.Net.Mail
classes to achieve this but I'm running into a strange problem:
1. I get an error message:
System.IO.IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.--->
System.Net.Sockets.SocketException: An existing connection was forcibly closed by
the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.
Read(Byte[] buffer, Int32 offset, Int32 size)
2. I tried sniffing the network activity to see what was going wrong. So here's how it goes:
i) The DNS lookup for my SMTP server's hostname works
ii) My application connects to the SMTP server and sends "EHLO MY-HOSTNAME"
iii) SMTP server responds back with it's usual
iv) My application sends "AUTH login abcdxyz" and receives an acknowledgement packet
At this point, it seems that either the SMTP server doesn't seem to request for the password or my machine shuts off the connection to the SMTP server before the SMTP server could request for a password.
I've tried using different SMTP ports and SMTP hosts. Also, I tried disabling my firewall and AV, but no luck. While connecting to my SMTP server using PuTTY and issuing the same sequence of commands as my application does (picked from the packet sniffer), everything works out fine and I'm able to send out the email.
Here's the code that I'm using:
Imports System.Net
Imports System.Net.Mail
Public Function SendMail() As Boolean
Dim smtpClient As New SmtpClient("smtp.myserver.com", 587) 'I tried using different hosts and ports
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New NetworkCredential("[email protected]", "password")
smtpClient.EnableSsl = True 'Also tried setting this to false
Dim mm As New MailMessage
mm.From = New MailAddress("[email protected]")
mm.Subject = "Test Mail"
mm.IsBodyHtml = True
mm.Body = "<h1>This is a test email</h1>"
mm.To.Add("[email protected]")
Try
smtpClient.Send(mm)
MsgBox("SUCCESS!")
Catch ex As Exception
MsgBox(ex.InnerException.ToString)
End Try
mm.Dispose()
smtpClient.Dispose()
Return True
End Function
Any advice?
Upvotes: 1
Views: 40908
Reputation: 287
In C# it works like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
MailAddress from = new MailAddress("[email protected]", "Name and stuff");
MailAddress to = new MailAddress("[email protected]", "Name and stuff");
List<MailAddress> cc = new List<MailAddress>();
cc.Add(new MailAddress("[email protected]", "Name and stuff"));
SendEmail("Want to test this damn thing", from, to, cc);
}
protected void SendEmail(string _subject, MailAddress _from, MailAddress _to, List<MailAddress> _cc, List<MailAddress> _bcc = null)
{
string Text = "";
SmtpClient mailClient = new SmtpClient("Mailhost");
MailMessage msgMail;
Text = "Stuff";
msgMail = new MailMessage();
msgMail.From = _from;
msgMail.To.Add(_to);
foreach (MailAddress addr in _cc)
{
msgMail.CC.Add(addr);
}
if (_bcc != null)
{
foreach (MailAddress addr in _bcc)
{
msgMail.Bcc.Add(addr);
}
}
msgMail.Subject = _subject;
msgMail.Body = Text;
msgMail.IsBodyHtml = true;
mailClient.Send(msgMail);
msgMail.Dispose();
}
}
Do not forget the using System.Net.Mail;
I Think in VB it works like this to, here is the code, it might have some errors, I don't often write in vb.net:
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim _from As New MailAddress("[email protected]", "Name and stuff")
Dim _to As New MailAddress("[email protected]", "Name and stuff")
Dim cc As New List(Of MailAddress)
cc.Add(New MailAddress("[email protected]", "Name and stuff"))
SendEmail("Wan't to test this thing", _from, _to, cc)
End Sub
Protected Sub SendEmail(ByVal _subject As String, ByVal _from As MailAddress, ByVal _to As MailAddress, ByVal _cc As List(Of MailAddress), Optional ByVal _bcc As List(Of MailAddress) = Nothing)
Dim Text As String = ""
Dim mailClient As New SmtpClient("Mailhost")
Dim msgMail As MailMessage
Text = "Stuff"
msgMail = New MailMessage()
msgMail.From = _from
msgMail.To.Add(_to)
For Each addr As MailAddress In _cc
msgMail.CC.Add(addr)
Next
If _bcc IsNot Nothing Then
For Each addr As MailAddress In _bcc
msgMail.Bcc.Add(addr)
Next
End If
msgMail.Subject = _subject
msgMail.Body = Text
msgMail.IsBodyHtml = True
mailClient.Send(msgMail)
msgMail.Dispose()
End Sub
Do not forget to import System.Net.Mail
Upvotes: 9
Reputation: 174
SendMail.CS Page
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace SendMailUsingWindowsForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Sending the email.
//Now we must create a new Smtp client to send our email.
SmtpClient client = new SmtpClient("smtp.gmail.com", 25); //smtp.gmail.com // For Gmail
//smtp.live.com // Windows live / Hotmail
//smtp.mail.yahoo.com // Yahoo
//smtp.aim.com // AIM
//my.inbox.com // Inbox
//Authentication.
//This is where the valid email account comes into play. You must have a valid email account(with password) to give our program a place to send the mail from.
NetworkCredential cred = new NetworkCredential("*******@gmail.com", "........");
//To send an email we must first create a new mailMessage(an email) to send.
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(textBox1.Text);//Nothing But Above Credentials or your credentials (*******@gmail.com)
// Recipient e-mail address.
Msg.To.Add(textBox2.Text);
// Assign the subject of our message.
Msg.Subject = textBox3.Text;
// Create the content(body) of our message.
Msg.Body = textBox4.Text;
// Send our account login details to the client.
client.Credentials = cred;
//Enabling SSL(Secure Sockets Layer, encyription) is reqiured by most email providers to send mail
client.EnableSsl = true;
//Confirmation After Click the Button
label5.Text = "Mail Sended Succesfully";
// Send our email.
client.Send(Msg);
}
catch
{
// If Mail Doesnt Send Error Mesage Will Be Displayed
label5.Text = "Error";
}
}
}
}
SendMail.Design
Upvotes: 7
Reputation: 1093
try to use this:
public SmtpClient client = new SmtpClient();
public MailMessage msg = new MailMessage();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
public void Send(string sendTo, string sendFrom, string subject, string body)
{
try
{
//setup SMTP Host Here
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
//converte string to MailAdress
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
//set up message settings
msg.Subject = subject;
msg.Body = body;
msg.From = from;
msg.To.Add(to);
// Enviar E-mail
client.Send(msg);
}
catch (Exception error)
{
MessageBox.Show("Unexpected Error: " + error);
}
}
dont forget to call:
using System.Net.Mail;
using System.Windows.Forms;
Upvotes: 0