user2936008
user2936008

Reputation: 1347

Sending mail in .NET -I am going wrong somewhere

I want to send a dummy email from my server in .NET to my mail .

I am just sending mail without manipulation : Please let me know where I am going wrong . I have written my code as below :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendingEmail.aspx.cs" Inherits="experiments_asp_SendingEmail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

 <form id="form1" runat="server">
<h2>Send e-mail to [email protected]:</h2>
<asp:Button Text="Submit" OnClick="sendMail" runat="server"/>
</form>

</body>
</html>

my .Net Code is shown as below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class experiments_asp_SendingEmail : System.Web.UI.Page
{
    protected void sendMail(object sender, EventArgs e)
    {
        Console.Write("came here");
        CreateTestMessage2("my college server"); //
    }
    public static void CreateTestMessage2(string server)
    {
        string to = "[email protected]";
        string from = "[email protected]";
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Using the new SMTP client.";
        message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
        SmtpClient client = new SmtpClient(server);       
        client.UseDefaultCredentials = true;

        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                  ex.ToString());
        }
    }
}

Please let me know if any issues in this code , Do i need to add anything extra.

I am not getting any error when i click on submit.

Expectation : to send a dummy mail . Please note I am new to <

Upvotes: 0

Views: 146

Answers (1)

Ckrempp
Ckrempp

Reputation: 324

I believe that you need to supply more information to the SMTP server to accept your request. Lets take Gmail for example. I have taken your code and enabled it to distribute emails off of the google smtp server:

string to = "[email protected]";
string from = "[email protected]";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = true;

NetworkCredential nc = new NetworkCredential("[email protected]", "yourPassword");
client.Credentials = nc;

try
{
    client.Send(message);
    MessageBox.Show("Your message was sent.");
}
catch (Exception ex)
{
    Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
          ex.ToString());
}

As you can see you need to specify the active port the smtp server is listening on, and allow SSL message transfer. The other key part is providing acceptable credentials the SMTP server can validate a user against for authentication. Maybe your college server has a similar architecture. Do you manage the server, or does someone else?

Upvotes: 2

Related Questions