如何在SQL Server中保存数据并发送电子邮件通知。 [英] How do I save data in SQL Server AND send an email notification.

查看:147
本文介绍了如何在SQL Server中保存数据并发送电子邮件通知。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我真的很难让这一点正确。我希望我的按钮能够将在webform上输入的数据保存到数据库中,同样的按钮也应该发送通知邮件。

这是我所得到的基本示例:

Hi,

I'm really struggling to get this one right. I want my button to SAVE the data entered on a webform into the database AND that same button should send a notification mail as well.
This is the basic example of what I've got:

private void button()
{
     Save();
     SendEmail();
}
private void SendEmail()
{
     //code here;
}
private void Save()
{
 //code here;

}



好​​的,现在是第一部分完美无缺,这就是SAVE方法()。 SendEMail部分是苦苦挣扎的开始。



首先关闭。选择下拉列表中的名称(此名称与该人的电子邮件地址对应)。然后其余部分填写。

数据库示例:


Okay, now the first part works perfectly fine, and that's the SAVE method(). The SendEMail part is where the struggling starts.

First off. A name in a drop down list is selected (this name corresponds with that person's email address). then the rest is filled in.
Example of a database:

ID      Name      Email
0       aaa       aaa@a.com





因此,一旦选择了下拉列表,就应该选择电子邮件地址。这部分也适合我。



现在,当我按下提交按钮保存数据并将通知发送给所选人时,它会给我一个SMTP例外。这是我的SendEmail()代码:





So once the dropdownlist is selected, is should select the email address. This part works fine for me too.

Now, when I press on the submit button which Saves data and sends the notification to the person selected, it gives me an SMTP Exception. This is my SendEmail() code:

     MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
    MailAddress fromAddress = new MailAddress(from);
    message.From = fromAddress;


    message.Subject = subject;
    message.IsBodyHtml = true;
    message.Body = body;
    smtpClient.Host = "mail.server.com";
    smtpClient.Port = 25;
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

    smtpClient.Send(message);
    msg = "Successful";





我很确定某些设置或某些东西仍然不对,因为我尝试了多行不同的代码确保电子邮件正常工作,但仍然会遇到异常。有时会给出超时时间。



是的,有人可以帮助我。



I am pretty sure some setting or something is still not right because I've tried multiple lines of different code to make sure the emails are working, but still get that exception. Sometimes it gives a Time Out Exception.

Yeah, so can anybody help me please.

推荐答案



通过这个 -

在数据库中插入记录后发送电子邮件 [ ^ ]






可能性:

1.我可能是SSL的案例。



您可以尝试:
Hi,

Possibilities :
1. I may be a case of SSL.

You could try :
smtpClient.EnableSSL=true;





2.检查端口和IP是否可从客户端PC /服务器访问



2. Check port and IP is accessible from the client pc /server


这篇关于如何在SQL Server中保存数据并发送电子邮件通知。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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