从 SQL Server 2008 数据库中提取电子邮件

发布于 2024-12-05 02:20:54 字数 198 浏览 1 评论 0 原文

我需要向所有订阅者发送一封电子邮件。我目前采用的是提供 SQL Server 2008 数据库的共享托管计划。

我真的不想将所有电子邮件复制到我的邮件客户端(雷鸟)中,也不想制作一个程序来从数据库中获取所有电子邮件并通过 smtp.net 发送它

我正在寻找一个免费的解决方案我可以告诉它数据库路径和电子邮件的列,并允许我编写主题和正文并让我发送它。

I need to send a email to all my subscribers. I am currently on a shared hosting plan that offers a SQL Server 2008 database.

I really don't want to copy all the emails into my mail client (thunderbird) nor do I want to make a program that would grab all the emails from the db and send it through smtp.net

I am looking for a free solution that I could just tell it the db path and the column of the emails and will allow me to write a subject and body and let me send it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-12-12 02:20:54
  1. 使用配置文件配置数据库邮件,该配置文件具有与您希望收件人看到的内容相匹配的发件人/回复信息。您可以在此处查看一个教程:
  1. 根据您是否想要向每个人发送相同的消息,或者对每个人进行个性化设置,您可以使用循环来构建将大块地址发送至密件抄送(您不一定希望一次发送给所有收件人)或为每个收件人制作个性化的正文/主题。

  2. 在循环的每次迭代中拥有正文和收件人后,您可以使用 msdb.dbo.sp_send_dbmail 发送每条消息。

编辑添加一些示例代码。

设置:

USE tempdb;
GO

CREATE TABLE dbo.Emails(Email VARCHAR(320));

INSERT dbo.Emails VALUES('[email protected]'),('[email protected]'),('[email protected]');

如果您只想发送所有相同的消息并假设列表很短 - 批处理或分块将是一个不同的问题:

DECLARE @bcc VARCHAR(MAX) = '';

SELECT @bcc += ';' + Email FROM dbo.Emails;

SET @bcc = STUFF(@bcc,1,1,'');

SELECT @bcc;

/*
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'profile name',
    @recipients = 'your address',
    @blind_copy_recipients = @bcc,
    @body = N'body',
    @subject = N'subject';
*/

- 如果您想向每个收件人发送单独的消息,请使用游标:

DECLARE @e VARCHAR(320);

DECLARE e CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
    FOR SELECT Email FROM dbo.Emails;

OPEN e;

FETCH NEXT FROM e INTO @e;

WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT 'Sending to ' + @e;

    /*
    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'profile name',
        @recipients = @e,
        @body = N'body',
        @subject = N'subject';
    */

    FETCH NEXT FROM e INTO @e;
END

CLOSE e;
DEALLOCATE e;

清理:

DROP TABLE dbo.Emails;

最后,如果您的收件人列表很大,我建议您寻找更适合批量邮件发送的其他替代方案。

  1. Configure database mail with a profile that has the from / reply-to information matching what you want your recipients to see. You can see one tutorial here:
  1. Depending on whether you want to send the same message to everyone, or personalize each one, you can use a loop to either build up chunks of addresses to BCC (you won't necessarily want to send to all the recipients at once) or to craft the individualized body/subject for each recipient.

  2. Once you have a body and recipient(s) in each iteration of the loop, you can use msdb.dbo.sp_send_dbmail to send each message.

EDIT adding some sample code.

Setup:

USE tempdb;
GO

CREATE TABLE dbo.Emails(Email VARCHAR(320));

INSERT dbo.Emails VALUES('[email protected]'),('[email protected]'),('[email protected]');

If you want to just send all the same message and assuming the list is short - batching or chunking would be a different question:

DECLARE @bcc VARCHAR(MAX) = '';

SELECT @bcc += ';' + Email FROM dbo.Emails;

SET @bcc = STUFF(@bcc,1,1,'');

SELECT @bcc;

/*
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'profile name',
    @recipients = 'your address',
    @blind_copy_recipients = @bcc,
    @body = N'body',
    @subject = N'subject';
*/

-- If you want to send an individual message to each recipient, use a cursor:

DECLARE @e VARCHAR(320);

DECLARE e CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
    FOR SELECT Email FROM dbo.Emails;

OPEN e;

FETCH NEXT FROM e INTO @e;

WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT 'Sending to ' + @e;

    /*
    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'profile name',
        @recipients = @e,
        @body = N'body',
        @subject = N'subject';
    */

    FETCH NEXT FROM e INTO @e;
END

CLOSE e;
DEALLOCATE e;

Cleanup:

DROP TABLE dbo.Emails;

Finally, if your list of recipients is large, I'll suggest you seek other alternatives that are better equipped for bulk mailing.

红焚 2024-12-12 02:20:54

ASP.net 有一个 SMTP 客户端类。我确信您可以从表中选择所有电子邮件地址,然后循环遍历它们,对每个地址调用 SMTP Send() 方法。

ASP.net has an SMTP client class. I'm sure you could select all the email addresses from your table then loop through them, calling the SMTP Send() method on each address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文