Print Page | Close Window

Sending Emails via SMTP/System.Net.Mail

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=29045
Printed Date: 28 March 2026 at 5:37pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Sending Emails via SMTP/System.Net.Mail
Posted By: IanSmithISA
Subject: Sending Emails via SMTP/System.Net.Mail
Date Posted: 02 January 2011 at 12:25pm

Good morning

I am going mad trying to send an email from my website, it worked on my test server but that is probably setup differently.

I did find http://www.webwiz.net/kb/webhosting/sending_web_form_mail.asp" rel="nofollow - http://www.webwiz.net/kb/webhosting/sending_web_form_mail.asp but I guess I am misunderstanding what it is telling me.

I have even put a page up at

http://www.iansmithcse.co.uk/MailTst1.aspx" rel="nofollow - http://www.iansmithcse.co.uk/MailTst1.aspx which is hosted here which allows me to type whatever I want, as this is a quicker test method, as well as select delivery method.

If I use IIS pickup I alway get "Cannot get IIS pickup directory.",

if I use network and putting values into From and To allows me to use an email address that is hosted to send to an email address hosted here, but I can not send to an non webwiz hosted address, I get the "Mailbox unavailable. The server response was: Authentication is required for relay " error.

If I put an non webwiz hosted address in the from box and a webwiz hosted address in the To box, the mail is sent as well.
 
Any suggestions would be greatfully received.

Thanks and bye

Ian Smith


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;

namespace TheVillage
{
    public partial class MailTst1 : System.Web.UI.Page
    {
        protected void PickupIIS_Click(object sender, EventArgs e)
        {
            SendMail(SmtpDeliveryMethod.PickupDirectoryFromIis);
        }
        protected void Network_Click(object sender, EventArgs e)
        {
            SendMail(SmtpDeliveryMethod.Network);
        }

        protected void SendMail(SmtpDeliveryMethod i32Method)
        {
            MailMessage mailMsg; SmtpClient mcMailClient;
            String strFrom, strTo, strSubject, strBody;
            String strServer, strUsername, StrPassword;
            System.Net.NetworkCredential ncCreds = new System.Net.NetworkCredential();

            strServer = this.txtMailServer.Text.ToString();
            strUsername = this.txtUserName.Text.ToString();
            StrPassword = this.txtPassword.Text.ToString();

            strFrom = this.txtFrom.Text.ToString();
            strTo = this.txtTo.Text.ToString();
            strSubject = "Test subject";
            strBody = "Test body";

            // Create a mail object
            mailMsg = new MailMessage(strFrom, strTo, strSubject, strBody);

            // Create a mail client
            mcMailClient = new SmtpClient();
            mcMailClient.UseDefaultCredentials = false;
            mcMailClient.DeliveryMethod = i32Method;
            mcMailClient.Host = strServer;

            // Set credentials
            ncCreds.Domain = strServer;
            ncCreds.UserName = strUsername;
            ncCreds.Password = StrPassword;
            mcMailClient.Credentials = ncCreds;

            // Send Issuer email
            mcMailClient.Send(mailMsg);
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MailTst1.aspx.cs" Inherits="TheVillage.MailTst1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="nofollow - http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns=" http://www.w3.org/1999/xhtml" rel="nofollow - http://www.w3.org/1999/xhtml " >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

Mail server <asp:TextBox runat="server" ID="txtMailServer" Text="mail.WebSiteLive.net"></asp:TextBox><br />
Email From <asp:TextBox runat="server" ID="txtFrom" Text=" " rel="nofollow - IanS@IanSmithCSE.co.uk"></asp:TextBox><br />
Email To<asp:TextBox runat="server" ID="txtTo" Text=" " rel="nofollow - IanS@IanSmithCSE.co.uk"></asp:TextBox><br />
Webwiz Mail Name<asp:TextBox runat="server" ID="txtUserName" Text="IanS"></asp:TextBox><br />
webwiz Mail Password <asp:TextBox runat="server" ID="txtPassword" Text=""></asp:TextBox><br />
<asp:LinkButton ID="lbLogin" runat="server" Width="150px" OnClick="PickupIIS_Click" TabIndex="10">Send Mail -

PickupIIS</asp:LinkButton><br /><br />
<asp:LinkButton ID="LinkButton1" runat="server" Width="150px" OnClick="Network_Click" TabIndex="10">Send Mail -

Network</asp:LinkButton><br />
   
    </div>
    </form>
</body>
</html>

 




Replies:
Posted By: 123Simples
Date Posted: 02 January 2011 at 7:02pm
Hi Ian
Are you just trying to create a contact form for the actual site, so people can fill it in, and you will get an email?
If so, contact me by PM and I'll see if I can give you the answer to do one for your site, as the code above is not going to work, and I'm not even sure how to understand it either


-------------
http://www.123simples.com/" rel="nofollow - Visit 123 Simples Web Design


Posted By: IanSmithISA
Date Posted: 03 January 2011 at 7:02am
Good morning
 
Thanks for the offer of help.
 
No I am not trying to create a  contact form, I just put it up as a form to show what I am doing.
The "real" page is at http://www.iansmithcse.co.uk/Login/Login.aspx?AppID=1" rel="nofollow - http://www.iansmithcse.co.uk/Login/Login.aspx?AppID=1  and "I am a new user" button
 
In practice it is part of a C# class that is used to send may different emails depending upon the action of the user within the site. Currently this site is being developed locally and I am checking that I can deploy it when ready.
 
The code works on my server and the servers that I used to use, so it is something pretty simple that is needs to be set to work with WebWiz's mail setup.
 
On my test sever I run both the mail server and IIS on the same server and I had a VPS with my previous host with the same arrangement. I plan to have a VPS here when I go live and get some traffic, but I am testing and starting of on a basic package.
 
Slightly off the orignal topic, I found that the VPS configurations offered by the previous host to be confusing. They offered cheap packages which never had the power to run a site, if you are gong to have a VPS you are almost cetain to want a database server and a mail server and RAM to usefully run these along with enough disc space to be able to run the updates.
 
Bye
 
Ian Smith


-------------
Why does anyone need more than an 80x25 character only VDU?


Posted By: WebWiz-Bruce
Date Posted: 03 January 2011 at 12:55pm
You should read through the following page again as it would appear that you are not reading it correctly:-

http://www.webwiz.net/kb/webhosting/sending_web_form_mail.asp" rel="nofollow - http://www.webwiz.net/kb/webhosting/sending_web_form_mail.asp

A pickup directory would only work if there is a localhost mail server. We do not run mail servers on our web servers so the pickup directly method would not work.

What you need to do is first create a mailbox in your Control Panel, this will be the 'from' address for your website to send emails.

The mail server that you would be using to send emails is mail.websitelive.net so you would enter this as the mail server host which you would be connecting to across a network.

Our mail servers require authentication to relay emails to non local mailboxes. What you would need to do is set the 'from' address as the mailbox you have created in your Control Panel and for the authentication use the username and password for this same mailbox (the username is the full email address).


-------------
https://www.webwiz.net/web-wiz-forums/forum-hosting.htm" rel="nofollow - Web Wiz Forums Hosting
https://www.webwiz.net/web-hosting/windows-web-hosting.htm" rel="nofollow - ASP.NET Web Hosting


Posted By: IanSmithISA
Date Posted: 05 January 2011 at 4:24pm
Good afternoon
 
Thank you for the reply.
 
What is driving me mad is that I think that I am doing this, but I keep getting the
 
"Mailbox unavailable. The server response was: Authentication is required for relay "
 
Finally worked it out.
 
            // Set credentials
            ncCreds.Domain = strServer;
            ncCreds.UserName = strUsername;
            ncCreds.Password = StrPassword;
            mcMailClient.Credentials = ncCreds;
 
Needs to be
 
            // Set credentials
            ncCreds.UserName = strUsername;
            ncCreds.Password = StrPassword;
            mcMailClient.Credentials = ncCreds;
 
Removed ncCreds.Domain entirely.
 
Bye
 
Ian


-------------
Why does anyone need more than an 80x25 character only VDU?


Posted By: lindajamison76
Date Posted: 13 September 2011 at 8:35am

Well, great work! You have helped me to improve my knowledge about this field. Thank you so much for sharing.

__________
http://moviesonlinewatch.net/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net