I'm trying to send an email from a web page, and I feel a bit lost and thought I'd come here for help, as I've spent enough time trying to get google to help me, now it's time for a human.
I've been provided with some login information for a server which should send email for me. This server info works for an existing website (made using asp classic) and I'm trying to reuse the same mail server for sending mail from my ASP.net page.
So here is a sample of vb code which contains the email server settings I've been provided with: (obviously I've changed them!!)
And it's written in vb unfortunately:
Dim errorMsg
If Len(request.form("Address"
)) > 1000 Then
errorMsg = ("Sorry, but there are too many characters in your Address, please <a href=""index.asp"" onsubmit=""history.back();
return false;"" onclick=""history.back();r
eturn false;"">go back</a> and try again.")
Else
For Field = 1 to Request.Form.Count - 4 'number of hidden fields + 1
FieldName = Replace(Request.Form.Key(F
ield),"_",
" ")
FieldValue = Request.Form.Item(Field)
Body = Body & FieldName & ":"& VbCrLf & FieldValue & "" & VbCrLf & VbCrLf
Next
'Dimension variables
Dim objCDOSYSCon
'Create the e-mail server object
Set objCDOSYSMail = Server.CreateObject("CDO.M
essage")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
'Set and update fields properties
With objCDOSYSCon
'Outgoing SMTP server
.Fields("
http://schemas.microsoft.com/cdo/configuration/smtpserver") = "111.111.111.111"
.Fields("
http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 11
'CDO Port
.Fields("
http://schemas.microsoft.com/cdo/configuration/sendusing"
) = 2
'Timeout
.Fields("
http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'email account details
.Fields.Item("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Fields.Item("
http://schemas.microsoft.com/cdo/configuration/sendusername") = "1111@dododo.com"
.Fields.Item("
http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypass111"
.Fields.Update
End With
'Update the CDOSYS Configuration
Set objCDOSYSMail.Configuratio
n = objCDOSYSCon
'Set and update email properties
With objCDOSYSMail
'0=Low, 1=Normal, 2=High
.Fields("urn:schemas:httpm
ail:import
ance").Val
ue = 1
'Who the e-mail is from
.From = Request.Form("email_addres
s")
'Who the e-mail is sent to
.To = "recipient@somecompany.com
"
'Who the e-mail is CC'd to
.Cc = "carbonrecipient@somecompa
ny.com"
'The subject of the e-mail
.Subject = Request.Form("email_subjec
t")
'Set the e-mail body format (HTMLBody=HTML TextBody=Plain)
.TextBody = Body
.Fields.Update
'Send the e-mail
.Send
End With
'Close the server mail object
Set objCDOSYSMail = Nothing
Set objCDOSYSCon = Nothing
'Rederect after sending email
Response.Redirect Request.Form("redirect_to"
)
End If
%>
Now I'd like to be able to use that stuff in my .net / C# web application. I've got a book... and here's what my book has made me do so far in my C# page:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls
;
using System.Net.Mail;
public partial class mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Net.Mail.MailMessag
e myMessage = new System.Net.Mail.MailMessag
e("mail1@s
omecompany
.com", "mail2@somecompany.com", "test, please ignore", "test, please ignore");
System.Net.Mail.SmtpClient
smtp = new System.Net.Mail.SmtpClient
("111.111.
111.111", 11);
System.Net.Mail.SmtpDelive
ryMethod smtpDelMethod = new SmtpDeliveryMethod(
smtp.Send(myMessage);
}
}
So I don't really see where to add my username and password. Plus I have no idea if it'll work even if i could figure out where to put a username and password.
Please help me to progress a bit!! Am I on the right track? Do I need to change the method i'm using?
Start Free Trial