在asp.net中有什么方法当我的网页在网页上打开时,应该生成一个带有用户名的电子邮件提醒,以便我有一个人正在访问我的页面的信息
我改变了像bellow
这样的代码 Public Sub SendMail(ByVal id As String)
Dim message = New MailMessage()
message.IsBodyHtml = True
message.From = New MailAddress("adeel.aslam0@gmail.com")
message.[To].Add(New MailAddress("malik.adeel@shakarganj.com"))
message.Subject = "user access page"
message.Body = "Your Message"
' add id here
Dim client = New SmtpClient("smtp.gmail.com")
client.Send(message)
End Sub
和像这样的web.config
<system.net>
<mailSettings>
<smtp from="adeel.aslam0@gmail.com">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="adeel.aslam0@gmail.com" password="pass" />
</smtp>
</mailSettings>
</system.net>
答案 0 :(得分:3)
你可能会做类似的事情......
修改您的web.config
文件以发送电子邮件
e.g。这使用gmail设置
<system.net>
<mailSettings>
<smtp from="yourMailId@gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="yourmail@gmail.com" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
在page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var user = HttpContext.Current.User;
if (user != null && user.Identity.IsAuthenticated)
{
var id = Membership.GetUser().ProviderUserKey;
SendMail(id.ToString());
}
}
}
public void SendMail(string id)
{
var message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("yourmail");
message.To.Add(new MailAddress("tomail"));
message.Subject= "user access page";
message.Body = "Your Message";// add id here
var client = new SmtpClient();
client.Send(message);
}
如果你使用vb
尝试这样的话。
这样,您无需更改web.config
Dim mailobject As New System.Net.Mail.MailMessage()
Dim myCred As New System.Net.NetworkCredential("yourmail@gmail.com", "password")
mailobject.To.Add("tomail")
mailobject.Subject = "User Access The Page"
mailobject.From = New System.Net.Mail.MailAddress("frommail")
mailobject.IsBodyHtml = True
mailobject.Body = "Your Message"
Dim SmtpMail As New System.Net.Mail.SmtpClient("smtp.gmail.com")
SmtpMail.UseDefaultCredentials = False
SmtpMail.EnableSsl = True
SmtpMail.Credentials = myCred
SmtpMail.Port = 587
SmtpMail.Send(mailobject)
答案 1 :(得分:0)
公平的问题。但除非你的访客数量非常有限,否则你真的不想这样做。请参阅上面的答案,了解如何操作。
最好将所有内容写入表中,并以每小时/每天的特定间隔向自己发送摘要。这样,您也可以使用该表格进行一些报告。