(ARTICLE) Sending email with c#
Well, I recently wrote a c# app sending email by connecting to known SMTP servers using System.Net.Mail, and sending mail from your account without having to connect to the site. The problem is I can send mail only from Gmail accounts. both yahoo, hotmail and another less know host Walla(from my country), fail to send emails and pop-up different errors. - when i try to connect to yahoo through my account i get this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: authentication required - for help go to http://help.yahoo.com/help/us/mail/pop/pop-11.html
- when i try to connect to walla through my account there i get this error: Mailbox unavailable. The server response was: 5.7.1 <targetmail@gmail.com>... Relaying denied. Please check your mail first or restart your mail session. I tried to set the next property: _client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; _client.PickupDirectoryLocation = @"c:\temp"; (_client is my SmtpClient) and tried to set the default Pickup directory to different paths manually. after setting it up, the program acts as if it has sent the massage successfully but it never gets to the 'SendCompleted' event and the email never gets to it's destination. I really need help guyz, if anyone can tell me y won't it work, i will b very grateful.
thanks. Now for the code:
This is my transportLayer class, handling all SmtpClient
methods.
Code: csharp
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace Mailer
{
public class TransportLayer
{
#region Members
private SmtpClient _client;
public event EventHandler<MessageSentEventArgs>
MessageSent;
#endregion
#region Ctors
public TransportLayer(string host,
Boolean useSecureConnection)
{
_client = new
SmtpClient(host);
_client.SendCompleted
+= new
SendCompletedEventHandler(_client_SendCompleted);
_client.EnableSsl
= false;
}
#endregion
#region Methods
public void SetCredentials(string
username, string password)
{
_client.UseDefaultCredentials
= false;
_client.Credentials
= new NetworkCredential(username, password,
_client.Host);
_client.DeliveryMethod
=
SmtpDeliveryMethod.PickupDirectoryFromIis;
_client.PickupDirectoryLocation
= @"c:\temp";
}
public void SendMessage(string from,
string to, string subject, string
body)
{
MailMessage
message;
message = new
MailMessage(from, to, subject, body);
_client.Send(message);
}
#endregion
#region Events
void _client_SendCompleted(object
sender,
System.ComponentModel.AsyncCompletedEventArgs e)
{
if (MessageSent
!= null)
{
MessageSent(this, new MessageSentEventArgs());
}
}
#endregion
}
}
COURTESY: www.go4expert.com

