0

I am New in Asp.net, i need to send email from Asp.net using my Outlook. I have one button in asp and when i click button(send) i want to send email. I tried to use Hotmail and Gmail but remote server in blocked. If you don't understand my question please tell me. I tried this:

         var smtpClient = new SmtpClient
        {
            Host = "outlook.mycompany.local",
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("myEmail@mycommpany.com", "myPassword")
        };

        var message = new System.Net.Mail.MailMessage
        {
            Subject = "Test Subject",
            Body = "FOLLOW THE WHITE RABBIT",
            IsBodyHtml = true,
            From = new MailAddress("myemail@mycommapny.com")
        };
        // you can add multiple email addresses here
        message.To.Add(new MailAddress("friendEmail@Company.com"));

        // and here you're actually sending the message
        smtpClient.Send(message);
}

Exeption Show: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated Please how can i do that ?

  • 3
    By Outlook, do you actually mean Exchange ? Outlook can't send emails if it's not connected to an email server, it's just a client. – Dimitar Dimitrov May 30 '13 at 13:11
  • You have to use real values for EmailFrom, EmailTo etc. Also make sure that EmailFrom is an address recognized and allowed by SMTP server, otherwise it will be blocked as a spam attempt – Yuriy Galanter May 30 '13 at 13:12
  • @DimitarDimitrov Yes it connected in email server. we are company and we use outlook to communicate.And i need to send email to everyone from asp. this is my task to do :S – Peterr Pann May 30 '13 at 13:15
  • @PeterrPann Sorry about that, my mind was blanking – Dan Drews May 30 '13 at 14:29

5 Answers5

1

Sending outbound email from an ASP.net web site can be problematic. Even if you get the SMTP information right, you still have to deal with:

  • Sender Policy Framework (SPF)
  • Whitelists/Blacklists
  • Validation
  • Bouncebacks

It's very difficult to do this yourself, which is why you might want to consider using a service provider instead. You simply use their API (often a REST call), and they do the rest. Here are three such providers:

Mandrill has a low-end free plan, and so does SendGrid if you are using it with Windows Azure. And they are all reasonably affordable, even for the larger plans.

I highly recommend using one of these with their own API instead of using System.Net.Mail yourself. But if you want, they also can act as an SMTP relay for you so you can use their SMTP servers and keep your System.Net.Mail code intact.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

First of all get the company SMTP server settings (from your sys admins I guess), then you can do something like this:

// setting up the server
var smtpClient = new SmtpClient
{
    Host = "your.company.smtp.server",
    UseDefaultCredentials = false,
    EnableSsl = true, // <-- see if you need this
    Credentials = new NetworkCredential("account_to_use", "password")
};

var message = new MailMessage
{
    Subject = "Test Subject",
    Body = "FOLLOW THE WHITE RABBIT",
    IsBodyHtml = true,
    From = new MailAddress("from@company.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("neo@matrix.com"));

// and here you're actually sending the message
smtpClient.Send(message);
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated .. Exeption ?? @Dimitar – Peterr Pann May 30 '13 at 13:44
  • Check username and password. And make sure you can get to the server. I am not able to get to my SMTP server from inside my work network. – jackncoke May 30 '13 at 13:46
  • @PeterrPann see if you need to enable SSL ? (again not sure about your server setup), also try a domain account with network credential -> `domain\\account`, `password`. In case you need SSL check my `SmtpClient` edit. – Dimitar Dimitrov May 30 '13 at 14:11
0

you can use this function. and one thing you have to store you email smtp login and password in web config file

/// <summary>
/// Send Email 
/// </summary>
/// <param name="strFrom"></param>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strAttachmentPath"></param>
/// <param name="IsBodyHTML"></param>
/// <returns></returns>
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
    Array arrToArray;
    char[] splitter = { ';' };
    arrToArray = strTo.Split(splitter);
    MailMessage mm = new MailMessage();
    mm.From = new MailAddress(strFrom);
    mm.Subject = strSubject;
    mm.Body = strBody;
    mm.IsBodyHtml = IsBodyHTML;
    //mm.ReplyTo = new MailAddress("replyto@xyz.com");
    foreach (string s in arrToArray)
    {
        mm.To.Add(new MailAddress(s));
    }
    if (strAttachmentPath != "")
    {
        try
        {
            //Add Attachment
            Attachment attachFile = new Attachment(strAttachmentPath);
            mm.Attachments.Add(attachFile);
        }
        catch { }
    }
    SmtpClient smtp = new SmtpClient();
    try
    {
        smtp.Host = ConfigurationManager.AppSettings["MailServer"].ToString();
        smtp.EnableSsl = true; //Depending on server SSL Settings true/false
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = ConfigurationManager.AppSettings["MailUserName"].ToString();
        NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"].ToString();
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;//Specify your port No;
        smtp.Send(mm);
        return true;
    }
    catch
    {
        mm.Dispose();
        smtp = null;
        return false;
    }

}
0

Try Amazon Simple Email Service (http://aws.amazon.com/ses/). If you're new to Amazon Web Services (AWS) there might be a learning curve. However, once you're familiar with their SDK which can be found on Nuget (AWSSDK) the process is very straight-forward (Amazon does have a lot of little wrapper classes which can be quirky).

So, to answer the question "How to send email?", it looks something like:

        var fromAddress = "from@youraddress.com";

        var toAddresses = new Amazon.SimpleEmail.Model.Destination("someone@somedestination.com");

        var subject = new Amazon.SimpleEmail.Model.Content("Message");

        var body= new Body(new Amazon.SimpleEmail.Model.Content("Body"));

        var message = new Message(subject , body);

        var client = ConfigUtility.AmazonSimpleEmailServiceClient;

        var request= new Amazon.SimpleEmail.Model.SendEmailRequest();

        request.WithSource(fromAddress)
                            .WithDestination(toAddresses)
                            .WithMessage(message );
        try
        {
            client.SendEmail(request);
        }
        catch (Amazon.SimpleEmail.AmazonSimpleEmailServiceException sesError)
        {
            throw new SupplyitException("There was a problem sending your email", sesError);
        }
BlackjacketMack
  • 5,472
  • 28
  • 32
-1

You can refer to below links:

http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

send email asp.net c#

I hope it will help you. :)

Community
  • 1
  • 1
Hitesh
  • 3,508
  • 1
  • 18
  • 24