Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Explained how to send email in different sites, but neither worked., some complex and some just did not work properly.
My website needs to send email to other.
Below is my source, But this code does not work. why?
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class SendDummyMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SmtpClient ob = new SmtpClient();
            MailMessage obMsg = new MailMessage();
            obMsg.To.Add(new MailAddress(to@abcd.com);
            obMsg.Subject = "Please Add subject";
            obMsg.Body = "this is  a test";
            obMsg.IsBodyHtml = true;            
            ob.Send(obMsg);

        }

        catch (Exception e1)
        {
            Response.Write(e1.Message);
        }

    }
}


web.config file
HTML
<system.net>
        <mailSettings>
            <smtp from="" >
                <network host="SmtpServer" port="25" defaultCredentials="true"/>
            </smtp>
        </mailSettings>
    </system.net>
Posted
Updated 24-Aug-12 22:09pm
v4
Comments
Richard MacCutchan 25-Aug-12 4:22am    
Is "SmtpServer" a real SMTP server system, or just a name?
me64 25-Aug-12 4:29am    
I just do not know, but i think that is just a name. i copy paste from http://www.techuncle.com/aspnet-c/75-how-to-send-a-mail-in-aspnet-35-using-smtp-client-.html
Richard MacCutchan 25-Aug-12 10:39am    
That does not seem like a very good idea, does it? If you wish to generate email messages then you must send them to a proper server in the first place; how else would you expect them to reach their destination? The server name you use must be the internet name of a proper SMTP server, not some random set of characters that has no meaning.
me64 25-Aug-12 13:39pm    
If you give me an example, I am grateful to you.

The first thing to do is to correct the host name. It's obviously not a real SMTP server name. If you have a SMTP server in your network, talk to the administrator to get the server name or the IP address.

Second thing is that if the SMTP server is expecting authentication, you should either define the credentials or run the process with credentials that are allowed to communicate with the SMTP server. The first option is quite typical one.

If you don't have a SMTP server in your network, consider using outside email system such as gmail.
 
Share this answer
 
Comments
me64 25-Aug-12 16:59pm    
How to use Gmail for this work. Is it fast? Do not advertise it?
Wendelius 25-Aug-12 17:16pm    
:) No I don't advertise any product or site.

Can't comment about the speed, but I guess that all the major players are fast.

To send an email via another site, have a look at these articles:
- Send Mail / Contact Form using ASP.NET and C#[^]
- Send Emails in ASP.NET using Gmail Credentials[^]
- Sending Email with Gmail, Yahoo, AOL, and Live Mail Via SMTP[^]
Probably, you need authorization to send the email. Have a look here - there is a working code example which is pretty simple to paste and use: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
Comments
Wendelius 25-Aug-12 14:58pm    
No actual reason for the downvote so countered.
I would sign up to a service like SendGrid.com and send emails through them. Keep the burden of sending emails off your network.

Here is an example of the code i would use:
C#
var smtp = new SmtpClient
{
Host = "smtp.sendgrid.net",
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Port = 25,
Credentials = new NetworkCredential("name@example.com", "password")
};
MailMessage thisMessage = new MailMessage(fromAddress, toAddress);
thisMessage.IsBodyHtml = true;
thisMessage.Subject = "Title Here";
thisMessage.Body = "Sample Body";
foreach (DataRow thisEmail in dtEmails.Rows)
{
thisMessage.Bcc.Add(thisEmail["AccountEmail"].ToString());
if (thisMessage.Bcc.Count > 500)
{
smtp.Send(thisMessage);
thisMessage.Bcc.Clear();
}
}
smtp.Send(thisMessage);
 
Share this answer
 
v2
Comments
Brandon Caruana 25-Aug-12 17:21pm    
You can also send something like 200 emails a day for free from sendgrid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900