Mahesh Wagh
Mahesh Wagh

Reputation: 161

Error In Sending Mail From Local Host To Gmail

I am just testing the email sending from local host to gmail.com

***Webconfig:***

<system.net>
<mailSettings>
  <smtp>
    <network 
      host="localhost"
       port="25"
        />
  </smtp>
</mailSettings>
</system.net>

***Default.aspx:***

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  Inherits="MasterApps._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>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />      

</div>
</form>
</body>
</html>

***Code behind is:***

protected void Button1_Click(object sender, EventArgs e)
{

        MailAddress from = new MailAddress("[email protected]");
        MailAddress to = new MailAddress("[email protected]");
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = "hi";
        msg.Body = "hello";
        SmtpClient sc = new SmtpClient();
        sc.Send(msg);

}

It’s fired error like:

*An exeception of type ‘System.Net.Mail.smtp failedReceipientExeception’ occurred in System.dll but was not handlled in user code. Additional Information: MailBox unavailable.The server response was:5.7.1. Unable to Relay for [email protected]

What’s wrong in above code? How to solve this?

Upvotes: 0

Views: 731

Answers (3)

Braks
Braks

Reputation: 92

Well in most cases the email fails to send because of the port number specified or because you didn't establist secure connection. Try this alternative. Click here you can download some class files for sending email to all domains. First add reference to EASendMail. And then code like this.

using EASendMail;
 SmtpMail oMail = new SmtpMail("Tryit");
                    SmtpClient oSmtp = new SmtpClient();

            //        
                    oMail.From = "eamil";

            //        // Set recipient email address
                    oMail.To = "[email protected]";

                    // Set email subject
                   oMail.Subject = "subject";

                    // Set email body
                    oMail.TextBody = "body";


                    SmtpServer oServer = new SmtpServer("smtp.gmail.com");


                    oServer.User = "email";
                    oServer.Password = "password";



                   oServer.Port = 465;

            //detect SSL type automatically
                 oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
                    oSmtp.SendMail(oServer, oMail);

Upvotes: 3

Braks
Braks

Reputation: 92

Try this. `

using (var client = new SmtpClient("smtp.gmail.com", 587))
            {
                client.Credentials = new NetworkCredential("[email protected]","password");
               var mail = new MailMessage();
                mail.From = new MailAddress("email");
                mail.To.Add("email");
                mail.Subject = "something";
                mail.Body = "body";
               client.Send(mail);

`

Upvotes: 0

Freelancer
Freelancer

Reputation: 9064

It can be possible that your mail server does not support anonymous to send mail.You just could send mail to the user who in your mail server.

Although edit your web.config code as below and then try:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network host="localhost"
                port="25"
                from="[email protected]"/>
        </smtp>
    </mailSettings>
</system.net>

Upvotes: 0

Related Questions