0

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 someone@example.com:</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 = "xyz@gmail.com";
        string from = "xyz@gmail.com";
        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 <

user2936008
  • 1,317
  • 5
  • 19
  • 42
  • 1
    Are you running this on your local machine, or on a hosted server? – trnelson Oct 23 '14 at 19:40
  • 2
    I guess your college server doesn't allow you to send mail from gmail accounts or it bounces for spam. – Patrick Hofman Oct 23 '14 at 19:40
  • @trnelson I am checking from my local , I didnt check from college server right now. – user2936008 Oct 23 '14 at 20:12
  • @PatrickHofman Please suggesr what i can do to send a dummy mail from local – user2936008 Oct 23 '14 at 20:12
  • When you post a question, make sure you tell us what exceptions your code generates, or how it doesn't behave as expected. Simply putting up a block of code and saying "fix it for me" isn't going to be very productive. – mason Oct 23 '14 at 20:28
  • One of my favorite tricks for working with SMTP on a local machine. Save SMTP mail to a directory: http://weblogs.asp.net/gunnarpeipman/asp-net-using-pickup-directory-for-outgoing-e-mails – trnelson Oct 24 '14 at 00:33
  • @mason , I am not getting any error : Even mentioned same in the post . Please read full question asked by user, getting neg rep discourage users . Thank you . Also since i am new to .Net , I am working hard to understand code. – user2936008 Oct 24 '14 at 06:53
  • @user2936008 You say you're not getting an error. But you're catching them and writing to the console. And unless you've done something special so you can see the content you write to the console, you're basically sending your error message down a black hole. For dev purposes, remove the try/catch block and view the error in VS. Or implement error logging, such as [ELMAH](https://code.google.com/p/elmah/). Or use `System.Diagnostics.Debug.WriteLine()` instead of `Console`. I think once you have implemented proper logging you see that you are *are* getting an error. – mason Oct 24 '14 at 12:16
  • @mason Sure , Thanks for the info , I am new to .NET . Until unless i know these information , how would you expect me to paste the error . If i would know this info i would have not posted in Stack overflow . Also no body will have doubts on .NET , the way you are explaining why you have gave me negative rep is simply illogical . – user2936008 Oct 24 '14 at 21:06

1 Answers1

2

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 = "xyz@gmail.com";
string from = "xyz@gmail.com";
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("yourEmail@gmail.com", "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?

Ckrempp
  • 324
  • 2
  • 9