Hi all ,
I developed a desktop sending email application,but it is generating an error when sending and the exception is saying : failure in sending mail.So is is a problem from the code or from my smtp server.?please help me,and this is my code down.
<pre lang="xml">using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Web;
namespace EmailHandler
{
public class Emailer
{
public static string SendMessage(string sendTo, string sendFrom,
string sendSubject, string sendMessage)
{
try
{
bool bTest = ValidateEmailAddress(sendTo);
if (bTest == false)
return "Invalid recipient email address: " + sendTo;
MailMessage message = new MailMessage(
sendFrom,
sendTo,
sendSubject,
sendMessage);
SmtpClient client = new SmtpClient(Properties.Settings.Default.SMTPAddress);
client.UseDefaultCredentials = true;
client.Send(message);
return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + ".";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
public static string SendMessageWithAttachment(string sendTo, string sendFrom,
string sendSubject, string sendMessage, ArrayList attachments)
{
try
{
bool bTest = ValidateEmailAddress(sendTo);
if (bTest == false)
return "Invalid recipient email address: " + sendTo;
MailMessage message = new MailMessage(
sendFrom,
sendTo,
sendSubject,
sendMessage);
foreach (string attach in attachments)
{
Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient(Properties.Settings.Default.SMTPAddress);
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(message);
return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + ".";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
public static bool ValidateEmailAddress(string emailAddress)
{
try
{
string TextToValidate = emailAddress;
Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}");
if (expression.IsMatch(TextToValidate))
{
return true;
}
else
{
return false;
}
}
catch (Exception)
{
throw;
}
}
}
}
<pre lang="xml">using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmailHandler;
namespace EmailTestApp
{
public partial class frmTestEmail : Form
{
ArrayList alAttachments;
public frmTestEmail()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string[] arr = openFileDialog1.FileNames;
alAttachments = new ArrayList();
txtAttachments.Text = string.Empty;
alAttachments.AddRange(arr);
foreach (string s in alAttachments)
{
txtAttachments.Text += s + "; ";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtSendTo.Text))
{
MessageBox.Show("Missing recipient address.", "Email Error");
return;
}
if (String.IsNullOrEmpty(txtSendFrom.Text))
{
MessageBox.Show("Missing sender address.", "Email Error");
return;
}
if (String.IsNullOrEmpty(txtSubjectLine.Text))
{
MessageBox.Show("Missing subject line.", "Email Error");
return;
}
if (String.IsNullOrEmpty(txtMessage.Text))
{
MessageBox.Show("Missing message.", "Email Error");
return;
}
string[] arr = txtAttachments.Text.Split(';');
alAttachments = new ArrayList();
for (int i = 0; i < arr.Length; i++)
{
if (!String.IsNullOrEmpty(arr[i].ToString().Trim()))
{
alAttachments.Add(arr[i].ToString().Trim());
}
}
if (alAttachments.Count > 0)
{
string result = Emailer.SendMessageWithAttachment(txtSendTo.Text,
txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text,
alAttachments);
MessageBox.Show(result, "Email Transmittal");
}
else
{
string result = Emailer.SendMessage(txtSendTo.Text,
txtSendFrom.Text, txtSubjectLine.Text, txtMessage.Text);
MessageBox.Show(result, "Email Transmittal");
}
}
}
}