脚本编写SMTP电子邮件问题 [英] Scripting SMTP email question

查看:170
本文介绍了脚本编写SMTP电子邮件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,如果像这个脚本这样的东西可以通过SMTP服务器 - 我在阅读了几个教程之后将它拼凑在一起。



我不能似乎在我的Contact.aspx文件中的Fullname字段中包含'FullName'(它总是生成错误),或者这些字段 - 用户的Fullname,Email,Subject和Body(Message) - 如何填充,因为它们是未知的。



脚本如下:





Can I ask, please, if something like this script would get through an SMTP server - I have pieced it together after reading a few tutorials.

I cannot seem to include 'FullName' from the Fullname field in my Contact.aspx file (it always generates an error), or how those fields - the user's Fullname, Email, Subject, and the Body (Message) - are populated because they are unknown.

The script looks like this:


Private Sub btnSend_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSend.Click

'use for external Web hosting service

        Dim smtp As New SmtpClient

'SmtpClient to send contact form - use Try/Catch block to trap sending errors

        Try
            Dim SmtpClient As New SmtpClient
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage

            SmtpClient.Host = "mail.server" 'Web hosting co server
            SmtpClient.Port = 25 'Web hosting co port
            smtp.Credentials = New Net.NetworkCredential("email", "password")
            SmtpClient.EnableSsl = False
            mail = New MailMessage()
            mail.From = New MailAddress("") 'user's email address
            mail.To.Add("info@mysite.com") 'Webmaster's email
            mail.Subject = ""
            mail.Body = ""
            SmtpClient.Send(mail)
            'MsgBox("mail send")

        Catch smtpExc As System.Net.Mail.SmtpException
            
        Catch ex As Exception
                     
        End Try

        Dim target = String.Format("~/thank.aspx?Name={0}", FullName.Text)

        'Redirect user to thank you page

        Response.Redirect(target, True)

    End Sub



感谢您的任何建议。


Thanks for any advice.

推荐答案

我在Classic ASP中做了类似的脚本:



I did a similar script in Classic ASP:

<![CDATA[<%
'Declaring Variables
Dim smtpserver,youremail,yourpassword,myCopy,ContactUs_Name,ContactUs_Email
Dim ContactUs_Body,Mail_Action,IsError
	
' Edit these 3 values accordingly
smtpserver = "mail.server"
youremail = "info@myplace.com"
yourpassword = "mypassword"
myCopy = "myemail@myplace.com"


' Grabbing variables from the form post
ContactUs_Name = Request("ContactUs_Name")
ContactUs_Email = Request("ContactUs_Email")
ContactUs_Body = Request("ContactUs_Body")
Mail_Action = Request("Mail_Action")

' Check email is valid
......

'Check for form field errors

'If no errors send mail

Dim ObjSendMail
	Set ObjSendMail = CreateObject("CDO.Message") 
     
	'Remote SMTP server configuration
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
.......

'End remote SMTP server configuration 

ObjSendMail.To = youremail
        ObjSendMail.CC = ContactUs_Email
        ObjSendMail.BCC = myCopy
	ObjSendMail.From = ContactUs_Email

ObjSendMail.HTMLBody = strBody

ObjSendMail.Send

'Begin HTML form fields





我希望能在ASP VB.NET中实现类似的功能,并且想知道我首次发布的脚本是否会实现它。我无法测试它,因为ASP.NET项目是未编译的。



我有一两个疑问。你如何编写发件人姓名的脚本(这是表单上的第一个字段),以及上面使用的SmtpServer和SmtpHost之间的区别。



再次感谢。% >]]>



I was hoping to achieve something like that but in ASP VB.NET, and wondered if the script I first posted would achieve it. I can't test it yet because the ASP.NET project is uncompiled.

I have one or two doubts. How would you script the sender's name (it's the first field on the form), and what is the difference between SmtpServer and SmtpHost as used above.

Thanks again.%>]]>


我已经设法整理了代码,但我仍然遇到了一些错误:



这是我表单中的名称字段:



I have managed to tidy up the code but I am still getting a couple of errors:

This is the name field in my form:

<asp:label runat="server" id="Label1" xmlns:asp="#unknown">Name</asp:label>
<asp:textbox runat="server" id="Name" columns="35" text="Name" xmlns:asp="#unknown" />





,希望有一些改进的代码:



and, hopefully, some improved code:

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click

        Dim myMessage As New MailMessage
        Dim Smtpserver As New SmtpClient
        Dim Name As String
        Dim Email As String
        Dim Subject As String
        Dim Message As String

        'Request.Form - extract data from form fields

        Dim Label1 As String = Request.Form("Name")
        Dim Label2 As String = Request.Form("Email")
        Dim Label3 As String = Request.Form("Subject")
        Dim Label4 As String = Request.Form("Message")

        'create the mail message

        myMessage.Name = Name
        myMessage.From = New MailAddress("Email") 'User's email
        myMessage.To.Add(New MailAddress("info@mysite.com")) 'Company email
        myMessage.Bcc.Add(New MailAddress("me@mysite.com")) 'My personal copy
        myMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
        myMessage.IsBodyHtml = True
        myMessage.Priority = MailPriority.High
        myMessage.Subject = Subject
        myMessage.Body = Message
        Smtpserver.DeliveryMethod = SmtpDeliveryMethod.Network
        Smtpserver.Host = ("Stmp.mysite.com")
        'Smtpserver.Host = System.Configuration.ConfigurationManager()
        Smtpserver.Port = 25

        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("email", "PWD")

        Smtpserver.Credentials = basicAuthenticationInfo
        Try
            Smtpserver.Send(myMessage)
            MsgBox("sent")
        Catch ex As Exception
            MsgBox(ex.Message)

            myMessage.Dispose()
            myMessage = Nothing
            Smtpserver = Nothing

        End Try

               Dim target = String.Format("~/thank.aspx?Name={0}", Name.Text)

        'Redirect user to thank you page

        Response.Redirect(target, True)

    End Sub
End Class





我收到一条错误消息:



I get an error that says:

'Text' is not a member of 'String'





这是指最后是页面重定向的文本,尽管我在代码的顶部声明了Text As String。



另一个错误很奇怪:



This refers to the 'Text' that is part of the page redirection at the end, although I have declared 'Text As String' at the top of the code.

The other error is strange:

'Name' is not a member of 'System.Net.Mail.MailMessage'.





System.Net.Mail.MailMessage不存在在解决方案资源管理器中的任何地方(我做过完整解决方案'查找'。这个STMP代码使用:在文件顶部导入System.Net.Mail。



有什么建议吗?



System.Net.Mail.MailMessage does not exist anywhere in Solution Explorer (I have done a full Solution 'Find'. This STMP code uses: Imports System.Net.Mail at the top of the file.

Any advice, please?


这篇关于脚本编写SMTP电子邮件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆