gpt4 book ai didi

c# - 从虚拟主机发送电子邮件而无需使用用户名和密码

转载 作者:可可西里 更新时间:2023-11-01 12:46:47 26 4
gpt4 key购买 nike

我写了一个 C# 程序来发送电子邮件,效果很好。此外,我还有一个用于发送电子邮件的 PHP 脚本,效果也很好。

但我的问题是:是否可以像在 PHP 中那样使用 C# 发送电子邮件,而无需指定凭据、服务器、端口等。

我想使用 C# 而不是 PHP,因为我正在创建一个 ASP.Net 网络应用程序。

这是我当前的 C# 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Mail;
using System.Net;

namespace $rootnamespace$
{
public partial class $safeitemname$ : Form
{
public $safeitemname$()
{
InitializeComponent();
}

private void AttachB_Click(object sender, EventArgs e)
{
if (AttachDia.ShowDialog() == DialogResult.OK)
{
string AttachF1 = AttachDia.FileName.ToString();
AttachTB.Text = AttachF1;
AttachPB.Visible = true;
AttachIIB.Visible = true;
AttachB.Visible = false;
}
}

private void AttachIIB_Click(object sender, EventArgs e)
{
if (AttachDia.ShowDialog() == DialogResult.OK)
{
string AttachF1 = AttachDia.FileName.ToString();
AttachIITB.Text = AttachF1;
AttachPB.Visible = true;

}
}





private void SendB_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient(EmailSmtpAdresTB.Text);
client.EnableSsl = true;
client.Timeout = 20000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(EmailUserNameTB.Text, EmailUserPasswordTB.Text);
MailMessage Msg = new MailMessage();
Msg.To.Add(SendToTB.Text);
Msg.From = new MailAddress(SendFromTB.Text);
Msg.Subject = SubjectTB.Text;
Msg.Body = EmailTB.Text;

/// Add Attachments to mail or Not
if (AttachTB.Text == "")
{
if (EmailSmtpPortTB.Text != null)
client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text);

client.Send(Msg);
MessageBox.Show("Successfuly Send Message !");
}
else
{
Msg.Attachments.Add(new Attachment(AttachTB.Text));
Msg.Attachments.Add(new Attachment(AttachIITB.Text));
if (EmailSmtpPortTB.Text != null)
client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text);

client.Send(Msg);
MessageBox.Show("Successfuly Send Message !");
}


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void settingsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.settingsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.awDushiHomesDBDataSet);

}

private void awDushiHomesEmail_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'awDushiHomesDBDataSet.Settings' table. You can move, or remove it, as needed.
this.settingsTableAdapter.Fill(this.awDushiHomesDBDataSet.Settings);

}
}
}

这是在 PHP 中的实现方式:

<?php
//define the receiver of the email
$to = 'test@hotmail.com';

//define the subject of the email
$subject = 'Test email with attachment';

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));

//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";

//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('PDFs\Doc1.pdf')));

//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="Doc1.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

我不是要你写我的代码,但也许你可以提供一些信息或让我知道是否可能。

编辑:

我的问题不是不使用 smtp 服务器,而是如何发送电子邮件而无需输入用户名和密码。我知道,只有当发送电子邮件的请求来 self 的服务器时,它才会起作用。

最佳答案

在 PHP 中您可以在不指定 SMTP 凭据的情况下发送邮件的原因是因为其他人已经为您配置了 php.ini 或 sendmail.ini(php 解释器用来获取某些值的文件)。

这通常是托管主机的情况(或者如果您在开发电脑上使用 php 和 AMPPS 等工具,它可以让您通过 UI 轻松编辑 SMTP 设置并忘记它)。

在 ASP.net/c# 中有 app.configweb.config 文件,您可以在其中注入(inject) smtp 设置(在 <mailSettings> 标记中)和因此实现与 PHP 相同的结果(SmtpClient 将自动使用存储在那里的凭据)。

查看这些问题的示例:

SmtpClient and app.config system.net configuration

SMTP Authentication with config file's MailSettings

关于c# - 从虚拟主机发送电子邮件而无需使用用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com