我通过外部SMTP服务器发送电子邮件。使用以下代码处理发送电子邮件:
try
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress(froma);
mail.To.Add(toc);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
SmtpClient smtp = new SmtpClient(ClientServer);
DataSet ds = new DataSet();
int retCode = Email.getSmtp(ref ds, DatabaseName);
string User="";
string Password="";
if (ds.Tables["Value"].Rows.Count > 0)
{
User = ds.Tables["Value"].Rows[0]["UserName"].ToString();
Password = ds.Tables["Value"].Rows[0]["PasswordName"].ToString();
}
else
{
MessageBox.Show("Invalid SMTP settings!");
return;
}
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(User, Password);
smtp.EnableSsl = false;
smtp.Send(mail);
}
catch (System.Web.HttpException exHttp)
{
System.Console.WriteLine("Exception occurred:" + exHttp.Message);
}
在我的服务器上测试此代码,在同一网络上使用我自己的SMTP服务器,这将返回我的所有电子邮件。但是,使用外部SMTP服务器会导致错误:
Mailbox unavailable. The server response was: relay not permitted.
我已经阅读过,看来SMTP的管理员必须允许我服务器的中继。但是,使用提供的身份验证凭据,我似乎无法连接,并且仍然收到中继错误。
答案 0 :(得分:5)
是的,听起来您正在使用的外部服务器不允许中继。即使您拥有正确的身份验证凭据,也无法发送电子邮件,因为仍然禁用了中继功能。你是这个外部服务器的管理员吗?如果您是,那么您可以启用它。这个LINK HERE解释了如何设置SMTP和中继。如果您不是此外部服务器的管理员,那么您必须联系谁,以便他们可以为您启用SMTP和中继。
听起来网络上的服务器已经安装了SMTP,并且因为您可以发送,所以继电器设置正确。我必须在这里安装SMTP并在所有三个服务器上配置中继(开发框,登台框和生产框)以发送电子邮件。我希望这个信息帮助。
答案 1 :(得分:4)
我有同样的错误并注释掉了:
public class DemoActivity extends Activity {
Button play;
MediaPlayer mp;
public int playstop = 0;
private Button extisaw;
public void onCreate(Bundle ww) {
super.onCreate(ww);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.saw);
mp = MediaPlayer.create(this, R.raw.raj);
show1();
show2();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (playstop == 0) {
MediaHandler(mp, 1);
mp.start();
playstop = 1;
} else {
MediaHandler(mp, 2);
mp.pause();
playstop = 0;
}
}
});
}
private void show2()
{
// TODO: Implement this method
this.extisaw =
(Button)this.findViewById
(R.id.about);
this.extisaw.setOnClickListener
(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});}
public void MediaHandler
(MediaPlayer mp, int e) {
if (e == 1) {
mp.start();
} else {
mp.pause();
}
}
public void show1() {
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[]{
R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4,
R.drawable.a5, R.drawable.a6,
R.drawable.a7, R.drawable.a8
, R.drawable.a9, R.drawable.a10, R.drawable.a11, R.drawable.a12
, R.drawable.a13, R.drawable.a14, R.drawable.a15
, R.drawable.a16, R.drawable.a17, R.drawable.a18, R.drawable.a19
, R.drawable.a20, R.drawable.a21, R.drawable.a22, R.drawable.a23
,
R.drawable.a24, R.drawable.a25,
R.drawable.a26, R.drawable.a27
, R.drawable.a29,
R.drawable.a30, R.drawable.a31
, R.drawable.a32, R.drawable.a33, R.drawable.a34, R.drawable.a35
, R.drawable.a36, R.drawable.a37, R.drawable.a38
, R.drawable.a39, R.drawable.a40, R.drawable.a42
, R.drawable.a43, R.drawable.a44, R.drawable.a45, R.drawable.a46,
R.drawable.a47, R.drawable.a48,
R.drawable.a49
, R.drawable.a51,
R.drawable.a52, R.drawable.a53
, R.drawable.a54, R.drawable.a55, R.drawable.a56
, R.drawable.a58, R.drawable.a59, R.drawable.a60
, R.drawable.a61, R.drawable.a62, R.drawable.a63
, R.drawable.a64, R.drawable.a65, R.drawable.a66, R.drawable.a67, R.drawable.a68, R.drawable.a69
, R.drawable.a70,
R.drawable.a71, R.drawable.a72
, R.drawable.a73, R.drawable.a74, R.drawable.a75
, R.drawable.a76, R.drawable.a77, R.drawable.a78
, R.drawable.a79, R.drawable.a80, R.drawable.a81
, R.drawable.a82, R.drawable.a83, R.drawable.a84, R.drawable.a85, R.drawable.a86, R.drawable.a87
, R.drawable.a88,
R.drawable.a89, R.drawable.a90
, R.drawable.a91, R.drawable.a93
, R.drawable.a94, R.drawable.a95, R.drawable.a96
, R.drawable.a97, R.drawable.a98, R.drawable.a99
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = DemoActivity.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
电子邮件已成功发送。