Good Morning All
I have the Following code that sends an e-mail, let me first post the markup and the code behind. The following is the markup of my page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
To <asp:TextBox ID="txtto" runat="server" Width="325px"></asp:TextBox><br />
<br />
From <asp:TextBox ID="txtFrom" runat="server" Width="326px"></asp:TextBox><br />
<br />
CC: <asp:TextBox ID="txtcc" runat="server" Width="326px"></asp:TextBox><br />
<br />
Subject:
<asp:TextBox ID="txtsubject" runat="server" Width="326px"></asp:TextBox><br />
Body :<br />
<asp:TextBox ID="txtbody" runat="server" Height="86px" Width="314px"></asp:TextBox><br />
<br />
Port:
<asp:TextBox ID="txtport" runat="server" Width="127px"></asp:TextBox><br />
<br />
Host
<asp:TextBox ID="txthost" runat="server" Width="127px"></asp:TextBox><br />
<br />
<asp:Button ID="btnSend" runat="server"
OnClick="btnSend_Click" Text="Send" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" /><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
and i have a Function that sends the e-mail like this
private static string SendEmail(String from, string to, string cc,int port, string subject, string message,String Host)
{
//create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//set the content
mail.From = new System.Net.Mail.MailAddress(from,"Test E-mail");
mail.Subject = subject;
mail.Body = message;
mail.IsBodyHtml = false;
//send the message
//SmtpClient smtp = new SmtpClient("196.35.193.28"); // use this Smtp Server
// seems we can use either the big routing server or our own ip address
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Port = port;
smtp.Host = Host;
//mailSenderInstance.Credentials = New System.Net.NetworkCredential("LoginAccout", "Password")
smtp.Credentials = new System.Net.NetworkCredential("administrator", "skskssk");
try
{
smtp.Send(mail);
return "Mail sent successfully!";
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
return errorMessage;
}
// Mailbox unavailable. The server response was 5.7.1. Unable to relay for (name).
// see notes under Techdocs
//client.Host = "localhost"; // setup the server / host sending the mail
// client.UseDefaultCredentials = true;
}
and in my button send we have the following
protected void btnSend_Click(object sender, EventArgs e)
{
Label1.Text = SendEmail(txtFrom.Text, txtto.Text, txtcc.Text, Convert.ToInt32(txtport.Text), txtsubject.Text,txtbody.Text,txthost.Text);
}
I have entered the Following in the Required Fields when i test the application
[B]To: [/B]Vuyiswa@its.co.za
[B]From:[/B]Vuyiswa@its.co.za
[B]CC:[/B]vuyiswamb@gmail.com
[B]
Subject:[/B] This is the Test for the mail in o!booking System
[B]Body[/B]If you Receive this e-mail Please confirm the receipt of this e-mail
[B]Port : [/B]25
[B]Host: [/B] mailserver.Vuyiswa.local
When i run my Application and clicking on the send button , i get the Following Error
System.InvalidOperationException: A recipient must be specified. at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.SendEmail(String from, String to, String cc, Int32 port, String subject, String message, String Host) in c:\Vuyiswa\Email_test\Default.aspx.cs:line 51
Thanks